As we can see above, we are creating a new class using the class operator and the class name. This is followed by an indented block that forms the body of the class. In this case, we have defined one method in the class.
Then we create an object / instance of that class using the class name and a couple of parentheses.
When we call a method on this object as myobject .method (arg1, arg2), it is automatically converted by Python to MyClass.method (myobject, arg1, arg2) — that's all about the special self.
The __init__ method
The __init__ method is similar to constructors in C++ and Java. It runs as soon as an object of the class is instantiated. This method is useful for doing whatever initialization you want to do on your object.
| tr> |
< code class = "undefined spaces">
Hello, my name is Shwetanshu
Here we define the __init__ method as a parameter name (along with the usual self). ,
Class and instance variables (or attributes)
In Python, instance variables — these are variables whose value is assigned within a constructor or method using self.
Class Variables — these are variables whose values are assigned in the class.
We can also define instance variables inside regular methods.
How to create an empty class?
We we can create an empty class with the
# Empty class
This article is courtesy of Shwetanshu Rohatgi . Please post comments if you find something wrong or if you would like to share more information on the topic under discussion