
Introduction to Python Object-Oriented Programming Concepts
Learn about classes, objectives, method definitions, and private variables in Python Object-Oriented Programming. Explore examples illustrating inheritance, method implementations, and handling private variables within classes.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Objectives O This chapter introduces 1. More method examples Private variables Inheritance 2. 3. 2
Method Definition O Class instances can have methods (defined by its class) for modifying its state class Class1: def Method1(self): ''' Method1 Statements ''' def Method2(self): ''' Method2 Statements ''' 3
Method Example O All attributes of a class that are function objects define corresponding methods of its instances class Person: def __init__(self, name): self.name = name def Say_Hello(self): print('Hello. My name is ' + self.name + '.') person1 = Person('Ken') person1.Say_Hello() #Hello. My name is Ken. 4
Method Example O The instance object is passed as the first argument of the function class Person: def __init__(self, name): self.name = name def Say_Hello(self, week): print('Hello. My name is ' + self.name + '.' + ' Today is ' + week + '.') person1 = Person('Ken') person1.Say_Hello('Monday') 5
Method Example class Person: def __init__(self, name): self.name = name self.friendList = [] person1 = Person('Ken') person2 = Person('John') person3 = Person('Mary') person4 = Person('Berry') def ChangeName(self, newName): print(self.name + " change his/her name to " + newName + ".") self.name = newName person1.AddFriend(person2) person1.AddFriend(person3) person4.AddFriend(person2) person3.ChangeName('Nancy') print('-----------------') person1.ShowFriends() person2.ShowFriends() person3.ShowFriends() person4.ShowFriends() def ShowFriends(self): print(self.name +"'s friends are:") for n in self.friendList: print(n.name) print() def AddFriend(self, member): self.friendList.append(member) member.friendList.append(self) print(self.name + ' and ' + member.name + ' are friends.') 6
Private Variables O Private variables or methods in python can be viewed and accessed within the class in which they have been declared and the classes outside that class cannot access them O A private variable (or method) is defined by defining a variable (or method) and prefixing its name with at underscores (__) least two leading 7
Private Variables class Person: def __init__(self, name): self.__name = name def __ShowName(self): print('My name is ' + self.__name) def ChangeName(self, newName): print(self.__name + " change his/her name to " + newName + ".") self.__name = newName self.__ShowName() person1 = Person('Ken') #print(person1.__name) AttributeError: 'Person' object has no attribute '__name' person1.ChangeName('Andy') 8
Inheritance O Inheritance allows us to define a class that inherits all the methods and properties from another class O Parent class is the class being inherited from, also called base class O Child class is the class that inherits from another class, also called derived class class DerivedClassName(BaseClassName) : <statement-1> <statement-N> 9
Inheritance class DerivedClassName(BaseClassName) : <statement-1> <statement-N> O The name BaseClassName must be defined in a scope containing the derived class definition O In place of a base class name, other arbitrary expressions are also allowed O For example, when the base class is defined in another module: class DerivedClassName(modulename. modulename.BaseClassName) : <statement-1> <statement-N> 10
Inheritance class DerivedClassName(BaseClassName) : <statement-1> <statement-N> O Execution of a derived class definition proceeds the same as for a base class O When the class object is constructed, the base class is remembered O If a requested attribute is not found in the class, the search proceeds to look in the base class O This rule is applied recursively if the base class itself is derived from some other class. 11
Example class Person(): def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname (self): print(self.firstname, self.lastname) x = Person("John", "Doe") x.printname() O Create a class named Student, which will inherit the properties and methods from the Person class class Student (Person): pass # Use the pass keyword when you do not want to add any other properties # or methods to the class y = Student( Mike , Olsen ) y.printname() 12
Example O If we add the __init__() function to the child class (instead of the pass keyword) O The child class will no longer inherit the parent's __init__() function O The child's __init__() function overrides the inheritance of the parent's __init__() function class Person(): def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname (self): print(self.firstname, self.lastname) class Student (Person): def __init__(self, fname, lname, gyear): Person.__ Person.__init init__(self, __(self, fname # parent s __init__() function by adding a call to the parent s __init__() function self.graduationyear self.graduationyear = = gyear gyear def printname (self): print(self.firstname, self.lastname, self.graduationyear) x = Student("John", "Doe", 2023) x.printname() fname, , lname lname) ) # You can keep the inheritance of the 13
Example O By using the super() function, you do not have to use the name of the parent element class Person(): def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname (self): print(self.firstname, self.lastname) class Student (Person): def __init__(self, fname, lname, gyear): super().__ super().__init init__( __(fname self.graduationyear = gyear def printname (self): print(self.firstname, self.lastname, self.graduationyear) x = Student("John", "Doe", 2023) x.printname() fname, , lname lname) ) 14
Another Example class Person(): def __init__(self, name): self.name = name def Hello(self): print(self.name + ': Hello.') class Student(Person): def __init__(self, name, sid): super().__init__(name) self.id = sid def Hello(self): super().Hello() print(self.name + ': I am a student.') print(self.name + ': My ID is ' + str(self.id) + '.') person1 = Student('Andy' , 102) person1.Hello() 15
Multiple Inheritance O Python supports a form of multiple inheritance O A class definition with multiple base classes looks like this: class DerivedClassName(Base1, Base2, Base3) : <statement-1> <statement-N> O For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy O If an attribute is not found in DerivedClassName, it is searched for in Base1, then (recursively) in the base classes of Base1, and if it was not found there, it was searched for in Base2, and so on. 16
Example class ClassA(): def Method1(self): print('ClassA method1') c = ClassC() c.Method1() # output 'ClassA method1' c.Method2() # output 'ClassB method2' class ClassB(): def Method1(self): print('ClassB method2') def Method2(self): print('ClassB method2') class ClassC(ClassA, ClassB): pass 17
Another Example ClassA class ClassA(): def Method1(self): print('ClassA method1') def Method2(self): print('ClassA method2') b = ClassB() b.Method1() b.Method2() print('-------------------') c = ClassC() c.Method1() c.Method2() c.Method3() print('-------------------') d = ClassD() d.Method1() d.Method2() d.Method3() d.Method4() ClassB ClassC class ClassB(ClassA): def Method2(self): print('ClassB method2') ClassD class ClassC(ClassA): def Method2(self): print('ClassC method2') def Method3(self): print('ClassC method3') 18 class ClassD(ClassB, ClassC): def Method4(self): print('ClassD method4')
Exercise O Please design a program for bank account management that enables to read the information of account ID, name, and balance from a file and show the information on the screen. The instructions include: O Input 0 Exit the program O Input 1 List all the information on the screen O Input 2 Store money by the specified account O Input 3 Withdraw money from the specified account Input file abc123 Mark 200 zxcv000 David 500 wxy987 Bob 300 19
O : O https://docs.python.org/3/tutorial/classes.ht ml O https://docs.python.org/3/library/functions. html#super O https://www.scaler.com/topics/python- private-variables/ O https://www.w3schools.com/python/python_ inheritance.asp 20