Unit 3
Unit 3
Object is simply a collection of data (variables) and methods (functions) that act on those data.
Variables define the attributes or properties and methods define the behavior. And, class is a
blueprint for the object.
To create a class, use the keyword class. Within the class, we define methods (functions) and
attributes (variables).
Methods and attributes defined in a class can be accessed by the instance variable or the object of
the class using dot notation.
Now let us define few variables inside the class A function defined in a class is called a
Employee() and get the variables from the method. An instance method requires an
instance of that class as shown in the following object/instance in order to call it
code: When creating an instance method, the first
parameter is always self. Though we can call it
class Employee: (self) by any other name, it is recommended to
name=”Amit” use self, as it is a naming convention.
desig=”Manager”
salary=50000 Example-
# Create first instance of Employee class class Employee:
emp1=Employee() name=”Amit”
print(emp1.name) desig=”Manager”
print(emp1.desig) salary=50000
print(emp1.salary)
# Create another instance of Employee class def getdata(self):
emp2=Employee() print(self.name)
print(emp2.name) print(self.desig)
print(emp2.desig) print(self.salary)
print(emp2.salary)
# Create first instance of Employee class
Output- emp1=Employee()
Amit print(emp1.getdata())
Manager”
50000 Output You can observe the following output
Amit when you execute the code given above:
Manager” Amit
50000 Manager
50000
Note:-We can access the object's Note: A class definition can include multiple
attributes/variables using the dot operator with functions. These functions represent the
object. functionality or behaviors that are associated
with the class.
Init Constructor
All classes have a function called __init__(), which is always executed when the class is being
initiated. Which is also called class constructor or initialization method that Python calls when
you create a new instance of this class.
We can use the __init__() function to assign values to object properties, or other
operations that are necessary to do when the object is being created:
The __init__ method is implicitly called as soon as an object of a class is instantiated.
This will initialize the object.
emp1=Employee()
The line of code shown above will create a new instance and assigns this object to the local
variable(object) emp1 and will call __init__ method implicitely.
The __init__() method can have single or multiple arguments for a greater flexibility. The init
stands for initialization, as it initializes attributes of the instance.
Note: The __init__() function is called automatically every time the class is being used to
create a new object.
Examples:-
Example 1- Output
Example-2 Output:
The self-Parameter: _The self parameter is a reference to the current instance of the class, and
is used to access variables that belongs to the class.
The first parameter to the __init__ method is called self and contains a reference to the
new object.
Any other arguments provided by the client are passed as additional parameters
*It does not have to be named self , you can call it whatever you like
Example
class Maths:
def subtract(self,i,j):
return i-j
def add(self,x,y):
return x+y
>>> m = Maths()
>>> m.subtract(10,5)
5
>>> m.add(6,7)
13
Note: When referring an instance method inside the class itself, we must always prefix the
method name with self
Example
class Maths:
def subtract(self,i,j):
return i-j
def testsub(self,x,y):
print(self.subtract(8,4))