In this article we will discuss about python instantiation and calling methods. We will also discuss how we can create and use objects in Python.
Python instantiation is the process of creating a new instance of a class. We generally invoke the constructor of a class for instantiating an object. For example, suppose we have a class name Employee. So we can create an instance of that class using the syntax such as:
new_employee = Employee().
Here new_employee is an instance or object of Employee class. Declaring new instance using above syntax indicates that we are invoking the constructor of employee class. If the constructor does require any parameters, we might use a syntax such as Employee(a, b, c) to construct a new instance. There are many Python built in classes also refers to as a literal form for designating new instances. For example, the command acceleration = 9.8 results in the creation of a new instance of the float class. The term 9.8 in the expression is a literal form. From a programmer’s perspective, yet another way to indirectly create a new instance of a class is to call a function that creates and returns such an instance. For example, Python has a built in function named sorted that takes a sequence of comparable elements as a parameter and returns a new instance of list class containing those elements in sorted order.
Python supports traditional functions that we can invoke with a syntax such as sorted(data), in this case data is parameter sent to the function. Python’s classes may also define one or more methods also refers to as member function, which we can invoke on a specific instance of a class using the dot (“.”) operator. For example to Python’s list class has a method named sot that can be invoked with a syntax such as data.sort(). This particular method rearranges the contents of the list so that they are sorted. Hare data is an object and upon which the sort method is invoked. We also can invoke a method upon the immediate result of some other operation (Often this will be an identifier). For example, if response identifies a string instance, the syntax response.lower().startswith(‘y’) first evaluates the method call, response.lower(). Which itself returns a new string instance, and then the startwith(‘y’) is called on that immediate string. When using a method of a class, it is important to understand its behavior. Some methods return information about the state of an object, but do not change that state. These are known as accessors. Other methods, such as the sort method of the list class, do change the state of an object. These methods are known as mutators or update methods.
NOT REVISED VERSION