Object Oriented Programming
Object Oriented Programming
January 3, 2021
def function(self):
print("This is a message inside the class.")
[2]: # We'll explain why you have to include that "self" as a parameter a little bit␣
,→later.
class MyClass:
variable = "GIFT University"
def function(self):
print("This is a message inside the class.")
1
myobjectx = MyClass()
# Now the variable "myobjectx" holds an object of the class "MyClass" that␣
,→contains the
# variable and the function defined within the class called "MyClass".
class MyClass:
variable = "GIFT University"
def function(self):
print("This is a message inside the class.")
myobjectx = MyClass()
myobjectx.variable
[4]: # So for instance the below would output the string "blah":
class MyClass:
variable = "GIFT University"
def function(self):
print("This is a message inside the class.")
myobjectx = MyClass()
print(myobjectx.variable)
GIFT University
[5]: # You can create multiple different objects that are of the same class(have the␣
,→same variables and functions defined).
# However, each object contains independent copies of the variables defined in␣
,→the class.
# For instance, if we were to define another object with the "MyClass" class␣
,→and then change the string in the
# variable above:
class MyClass:
variable = "GIFT University"
2
def function(self):
print("This is a message inside the class.")
myobjectx = MyClass()
myobjecty = MyClass()
myobjecty.variable = "Gujranwala"
GIFT University
Gujranwala
class MyClass:
variable = "GIFT University"
def function(self):
print("Hello, Welcome to AI Lab")
myobjectx = MyClass()
myobjectx.function()
# The above would print out the message, "This is a message inside the class."
class Coin:
# The _ _init_ _ method initializes the
# sideup data attribute with 'Heads'.
def __init__(self):
3
self.__sideup = 'Heads'
# The toss method generates a random number
# in the range of 0 through 1. If the number
# is 0, then sideup is set to 'Heads'.
# Otherwise, sideup is set to 'Tails'.
def toss(self):
if random.randint(0, 1) == 0:
self.__sideup = 'Heads'
else:
self.__sideup = 'Tails'
def get_sideup(self):
return self.__sideup
Take a closer look at the header for each of the method definitions (lines 10, 17, and 26) and notice
that each method has a parameter variable named self:
Line 10: def _ _init_ _(self):
Line 17: def toss(self):
Line 26: def get_sideup(self):
In object-oriented programming that a method operates on a specific object’s data attributes. When
a method executes, it must have a way of knowing which object’s data attributes it is supposed to
operate on. That’s where the self parameter comes in. When a method is called, Python makes
the self parameter reference the specific object that the method is supposed to operate on.
Let’s look at each of the methods. The first method, which is named **_ init _**, is defined in
lines 10 through 11:
def __init__(self):
self.__sideup = 'Heads'
Most Python classes have a special method named _ init , which is automatically executed when an
instance of the class is created in memory. The init _ method is commonly known as an initializer
method because it initializes the object’s data attributes.
5 Private Variable
“Private” instance variables that cannot be accessed except from inside an object don’t exist in
Python. Look at the above code line no 11
def __init__(self):
self.__sideup = 'Heads'
__sideup variable is a private variable of class Coin
4
6 main function for Coin Class
[8]: # The main function.
def main():
# Create an object from the Coin class.
my_coin = Coin()
# Display the side of the coin that is facing up.
print('This side is up:', my_coin.get_sideup())
self.__emp_id = 12345
self.__name = "Ali"
self.__salary = 5000.50
5
return " The id of emplyee is: {} \n The name of employee is: {} \n␣
,→The salary of employee is: {}".format(self.__emp_id,self.__name,self.
,→__salary)
def main():
emp = Employee()
print(emp)
if "__name__ == main":
main()
8 Inheritance
[10]: # The Automobile class holds general data
# about an automobile in inventory.
class Automobile:
6
# for the class's data attributes.
def get_make(self):
return self.__make
def get_model(self):
return self.__model
def get_mileage(self):
return self.__mileage
def get_price(self):
return self.__price
class Car(Automobile):
7
# The car is a 2007 Audi with 12,500 miles, priced
# at $21,500.00, and has 4 doors.
used_car = Car('Audi', 2007, 12500, 21500.00, 4)
Make: Audi
Model: 2007
Mileage: 12500
Price: 21500.0
Number of doors: 4
9 Exercise
9.1 Employee Class
Write a class named Employee that holds the following data about an employee in attributes: name,
ID number, department, and job title.
Once you have written the class, write a program that creates three Employee objects to hold the
following data:
he program should store this data in the three objects and then display the data for each employee
on the screen.
8
Description Units in Inventory Price
Item #1 Jacket 12 59.95
Item #2 Designer Jeans 40 34.95
Item #3 Shirt 20 24.95
9
whether the customer wishes to be on a mailing list. Demonstrate an instance of the Customer
class in a simple program.
[ ]:
10