It has two functions, _init_() to initialize the variables (defaults to zero) and get_data() to display the number properly.Īn interesting thing to note in the above step is that attributes of an object can be created on the fly. In the above example, we defined a new class to represent complex numbers. # AttributeError: 'ComplexNumber' object has no attribute 'attr'ĪttributeError: 'ComplexNumber' object has no attribute 'attr' # but c1 object doesn't have attribute 'attr' We normally use it to initialize all the variables. This type of function is also called constructors in Object Oriented Programming (OOP). This special function gets called whenever a new object of that class is instantiated. Of one particular interest is the _init_() function. Now you must be familiar with class object, instance object, function object, method object and their differences.Ĭlass functions that begin with double underscore _ are called special functions as they have special meaning. It can be named otherwise but we highly recommend to follow the convention. In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method's object before the first argument.įor these reasons, the first argument of the function in class must be the object itself. This is because, whenever an object calls its method, the object itself is passed as the first argument. You may have noticed the self parameter in function definition inside the class but we called the method simply as eet() without any arguments. This means to say, since eet is a function object (attribute of class), eet will be a method object. Methods of an object are corresponding functions of that class. We can access the attributes of objects using the object name prefix.Īttributes may be data or method. This will create a new object instance named harry. The procedure to create an object is similar to a function call. It can also be used to create new object instances (instantiation) of that class. We saw that the class object could be used to access different attributes. This class object allows us to access the different attributes as well as to instantiate new objects of that class. For example, _doc_ gives us the docstring of that class.Īs soon as we define a class, a new class object is created with the same name. There are also special attributes in it that begins with double underscores _. I have created a new class'''Ī class creates a new local namespace where all its attributes are defined. Although not mandatory, this is highly recommended. The first string inside the class is called docstring and has a brief description of the class. Like function definitions begin with the def keyword in Python, class definitions begin with a class keyword. An object is also called an instance of a class and the process of creating this object is called instantiation. House is the object.Īs many houses can be made from a house's blueprint, we can create many objects from a class. Based on these descriptions we build the house. It contains all the details about the floors, doors, windows, etc. We can think of a class as a sketch (prototype) of a house. Similarly, a class is a blueprint for that object. Unlike procedure-oriented programming, where the main emphasis is on functions, object-oriented programming stresses on objects.Īn object is simply a collection of data (variables) and methods (functions) that act on those data. Python is an object-oriented programming language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |