![]() ![]() “self” is the first parameter in this method just like any other method although in case of init, “self” refers to a newly created object unlike other methods where it refers to the current object or instance associated with that particular method.įollowing example illustrates how to use the _init_() function: class IntellipaatClass:.Like any other method, init method starts with the keyword “def”.It is automatically called when you create an object using a class and is used to initialize the variables of the class. In python classes, “_init_” method is reserved. Go for this in-depth job-oriented Python Training in Hyderabad now! You can delete an object by using del keyword You can modify an object like this: Person.age=20 It can be named something else too, but naming it ‘self’ is a convention and it is considered as a good practice to follow this convention. That’s why the very first argument in the function must be the object itself, which is conventionally called ‘self’. That is because, when a function is called using an object, the object itself is passed automatically to the function as an argument, so object1.function1() is equivalent to object1.function1(object1). We must notice that we are using a parameter named self while defining the function in the class, but we’re not really passing any value while calling the function. Print(‘Welcome to Intellipaat’)#creating a new object named object1 using class object That is not all the class object is used for it can also be used to create new objects, and then those objects can be used to access the attributes as shown in the following example: class IntellipaatClass: We saw in the previous topic that the class object of the same name as the class is used to access attributes. ![]() #accessing attributes using the class object of same name As soon as the class is created, a class object is also created which is used to access the attributes in the class, let us understand this with the help of a Python class example. The create class statement will create a local namespace for all the attributes including the special attributes that start with double underscores (_), for example, _init_() and _doc_(). The following example illustrates how to define a class in python: class IntellipaatClass: Although the use of docstrings is not mandatory, it is still recommended as it is considered to be a good practice to include a brief description of the class to increase the readability and understandability of the code. Much similar to functions, we use docstrings in classes as well. Just as a function in Python is defined using the def keyword, a class in Python is also defined using the class keyword, followed by the class name. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |