Class in Python is declared using the class keyword.To declare a simple class called Example you use the following statement
class Example: pass
we used pass since we are not declaring the class body.
We can modify the above class to add a simple method called PrintHello.What this method will do is print a simple message. __init_ is the class constructor.
class Example: def __init__(self): print("Example object is created") def PrintHello(self): self.name="1" print("hello world")
If you observe the above class definition you can see that __init__ method as well as the PrintHello() method are passed a parameter called self.The keyword self represents an object or instance of the class.The current object is represented by the keyword self.
self parameter is used in a method in a class.You can not use self parameter outside the class.
In the PrintHello() method we are adding a new attribute called name to self.This is one way of adding attributes to an object.We can access the attribute name from the calling method as:
ob=Example() ob.PrintHello() print(ob.name) #this will print the value "1"