EMAIL SUPPORT
dclessons@dclessons.comLOCATION
AFClasses & Objects in python
How to create Class in Python
Class is created by using word class along with class name, and then object is written inside classname.
Let’s understand this by an example below given.
Here keyword class is used to create class along with classname called ‘Emp’. In this class we will define the class attributes and methods. So we are defining “Emp” attributes as name, age and salary. So in order to declare this, as we also know that in Python we cannot declare variables, so we are using a special method __init__. This method is used to initialize the variables.
Under this method, we are also calling self variable, which refers to current class instance. When we create an instance for Emp class, a bunch of memory block is allocated and this memory location is by default stored in self.
The instance contains the variable ‘name’, ‘age’, ‘salary’ and these are called instance variable.
Now we will again declare the method called display, which will take the self variable as parameter and used to display the values of the variable by referring them using self.
Once the program is run, we will get following output.
- Hi I am Parth
- Hi my age is 5
- Hi my salary is 2000
Let’s understand another example of class, where we create an instance to class.
Here, we are creating and instance by using below syntax:
Instance name = Classname ()
To create an instance to Emp class, we use below code
e1 = Emp ()
Here e1 is an instance name, once this is done, a block of memory is allocated on heap. Once memory is allocated, a special method __int__ (self) is called internally, and this method stores data in to variables. Because this method is useful to construct instance, it is called constructor.
Finally, allocated memory location address of instance is returned to e1 variable.
Self Variable
This is the default variable that contains memory address of the instance of the current class. Once instance to the class is created, instance name contains memory location of instance and this memory location is internally passed to self as shown below:
e1 = Emp()
E1 contains memory address of the instance, this memory address is by default passed to ‘self’ variable. There are two ways to use self variable.
- def __init__(Self):
- In this case , self is used to refer the instance variable inside constructor
- def display(self)
- Here self can be used as first parameter in the instance method.
Constructor
A constructor is a method used to initialize the instance variable of a class. Here we create the instance variable and initialize them with some values. The first parameter of the constructor will be self variable that contains memory address of the instance.
def __init__(self):
self.name = Parth
self.salary = 20000
A constructor is called at time of creating an instance. The constructor will be called when we create an instance as :
E1 = Emp()
Here e1 is name of instance, the empty parenthesis after class shows that no values are being passed to constructor. Now if you want to pass the values to constructor then we should use below syntax:
def __init__(self, n =’’, m = 0)
self.name = n
self.salary = m
Here we are passing argument n and m, whose default values are ‘‘(none) and 0.
Since we are not passing any values to the instance (e1 = Emp ()), none and zero will be stored in to name and salary.
Now let’s suppose if we create instance like e1 = Emp(‘Parth’, 8000) , here we are passing two arguments which has values. Now these values will be sent to argument n and m and from there it is stored in to name and salary.
Below example shows the program to create employee class with a constructor having more than one parameter.
Types of variable
There are 2 ways by which variables are provided inside a class.
- Instance variable
- Class variable or static variable.
LEAVE A COMMENT
Please login here to comment.