5-6
5-6
5-6
Programming
Dr. Bharat Singh
PhD, CSE
Contents
• Accessing Class Members
• Class Attribute
• Class Method
• Defining Funtion in class
• Destructor
• Self argument
Write a Python program to create a Vehicle class
with max_speed and mileage instance attributes.
class Vehicle:
def __init__(self, max_speed, mileage):
self.max_speed = max_speed
self.mileage = mileage
def getPerimeter(self):
return 2*self.radius*math.pi
def getArea(self):
return math.pi*self.radius*self.radius
def setradius(self, radius):
self.radius=radius
c2=Circle(5)
print("the area of the circle of radius ",c2.radius, "
is" ,c2.getArea())
c3=Circle(25)
c4=Circle(125)
Write a Python program to create a calculator class.
Include methods for basic arithmetic operations.
# Define a class called Calculator to
perform basic arithmetic operations
class Calculator:
# Define a method for addition that
takes two arguments and returns their
sum
def add(self, x, y):
return x + y
def subtract(self, x, y):
return x - y
def multiply(self, x, y):
return x * y
def divide(self, x, y):
if y !=0:
return x / y
else:
return("Cannot divide by zero.")
calculator = Calculator()
result = calculator.add(7,5)
print("the sum of 7 + 5 =", result)
Example class Car():
# init method or constructor
def __init__(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model is", self.model )
print("color is", self.color )
instance_name.attribute_name.
• A class variable is accessed or modified using the class name
class Student:
# class variables
school_name = 'ABC School'
# constructor
def __init__(self, name, age):
# instance variables
self.name = name
self.age = age
s1 = Student("Harry", 12)
# access instance variables
print('Student:', s1.name, s1.age)
def show(self):
print("Fruit is", self.name, "and Color is", self.color)
# delete object
del emp
# constructor
def __init__(self, name):
print('Inside Constructor')
self.name = name
print('Object initialized')
def show(self):
print('Hello, my name is', self.name)
# destructor
def __del__(self):
print('Inside destructor')
print('Object destroyed')
# create object
s1 = Student('Emma')
s1.show()
# delete object
del s1
Important Points to Remember
about Destructor
• The __del__ method is called for
class Vehicle:
any object when the reference def __init__(self, speed):
count for that object becomes zero. self.speed = speed;
• The reference count for that object def __del__(self):
becomes zero when the application print('Release resources')
ends, or we delete all references
# creating an object
manually using the del keyword. car = Vehicle(350);
• The destructor will not invoke when
# to delete the object explicitly
we delete object reference. It will del car
only invoke when all references to
the objects get deleted.
The self Parameter Python
• The class contains instance variables and methods. class GFG:
def __init__(self, name, company):
• Whenever we define instance methods for a class,
self.name = name
we use self as the first parameter, but it is not used self.company = company
when the method is called.
• self is a parameter that references the object itself. def show(self):
print("Hello my name is " + self.name+" and I" +
• Using self, we can access the instance variable and " work in "+self.company+".")
instance method of the object.