08 Class II Inheritance
08 Class II Inheritance
Class II – Inheritance
inheritance, name mangling, printing objects
Prof. Hyeong-Seok Ko
Seoul National University
Dept. of Electrical and Computer Engineering
Contents
• Inheritance
• Name Mangling
• Printing the Content of a Object
Syntax of Inheritance
class Person:
def __init__(self, name, phoneNumber):
self.name = name
self.phoneNumber = phoneNumber base class
= superclass
def print_info(self):
print("Name: ", self.name, ", Phone Number: ", self.phoneNumber)
class Student(Person):
def __init__(self, name, phoneNumber, subject, studentID):
super().__init__(name, phoneNumber) derived class Person
self.subject = subject = subclass
self.studentID = studentID
Student
Person
class Person:
name = "Default Name"
Person ... name
def print_info(self): print_info
...
hakbun
print_grade
Reusing Superclass Properties
class Person:
def __init__(self, name, phoneNumber):
self.name = name
self.phoneNumber = phoneNumber base class
= superclass
def print_info(self):
print("Name: ", self.name, ", Phone Number: ", self.phoneNumber)
class Student(Person):
def __init__(self, name, phoneNumber, subject, studentID):
super().__init__(name, phoneNumber) derived class
self.subject = subject = subclass
self.studentID = studentID
class Student(Person):
def __init__(self, name, phoneNumber, subject, studentID):
# super().__init__(name, phoneNumber) derived class
self.subject = subject = suberclass
self.studentID = studentID
class Student(Person):
def __init__(self, name, major):
super().__init__(name)
self.major = major
p = Person("Kim")
s = Student("Kim", "ECE")
print(Person.__bases__)
print(Student.__bases__)
print(p.__class__.__bases__)
print(s.__class__.__bases__)
Variable Overriding
• By inheritance, the subclass inherits the variables from the superclass.
• But there are cases when the superclass variable doesn’t quite fit the subclass.
• In such a case, Python allows us to define a variable in the subclass with the same name as
the one in the superclass, which is called variable overriding.
class Student(Person):
def __init__(self, name, major):
super().__init__(name)
self.major = major
p = Person("Kim")
s = Student("Kim", "ECE")
print(Person.mro())
print(Student.mro())
print(p.__class__.mro())
print(s.__class__.mro())
class Student(Person):
def __init__(self, name, phoneNumber, subject, studentID):
super().__init__(name, phoneNumber) derived class
self.subject = subject = suberclass
self.studentID = studentID
p = Person("Tom", "011-")
s = Student("Marry", "010-", "CS", "2012")
l = [p, s]
class Person:
def __init__(self, name, phoneNumber):
self.name = name
self.phoneNumber = phoneNumber base class
= superclass
def print_info(self):
print("Name : ", self.name, ", Phone Number : ", self.phoneNumber)
class Student(Person):
def __init__(self, name, phoneNumber, subject, studentID):
# super().__init__(name, phoneNumber) derived class
self.subject = subject = suberclass
self.studentID = studentID
class Person:
def __init__(self, name, phoneNumber): p = Person("Tom", "011-")
self.name = name s = Student("Marry", "010-", "CS", "2012")
self.phoneNumber = phoneNumber l = [p, s]
def print_info(self): for item in l :
print("Name : ", self.name, ", Phone Number : ", self.phoneNumber) print(dir(item))
class Student(Person):
def __init__(self, name, phoneNumber, subject, studentID):
# super().__init__(name, phoneNumber)
self.subject = subject
self.studentID = studentID
There is no 'name' or 'phoneNumber' for s.
b = B() b = B()
b.Print() b.Print()
print(dir(b))
A’ x B’s x
More about Name Mangling
• A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid
naming conflicts in subclasses.
• This is called name mangling—the interpreter changes the name of the variable in a way that makes it
harder to create collisions when the class is extended later.
• In Experiment 2, in the attribute list of t, both foo and _bar appear but __baz does not.
– Instead, there is an attribute called _X__baz.
– This is the name mangling that Python applies, in order to protect the variable from getting overridden in the
subclasses.
– Note that, within the class definition, __baz can be freely used.
– Outside the class definition, however, it must be referred with _X__baz.
• In Experiment 3, note that the original _X__baz is also still around.
Experiment 2
class X: Experiment 3
def __init__(self): class Y(X): >>> t2 = Y()
self.foo = 11 def __init__(self): >>> t2._bar
self._bar = 22 super().__init__() 222
self.__baz = 33 self.foo = 111 >>> t2.__baz
self._bar = 222 AttributeError: "'Y' object has no attribute '__baz'“
>>> t = X() self.__baz = 333 >>> dir(t2)
>>> dir(t) ['_Y__baz', '_X__baz',..., '_bar', 'foo']
['_X__baz',..., '_bar', 'foo'] >>> t2._Y__baz
>>> print(t._X__baz) 333
33 >>> t2._X__baz
>>> print(t.__baz) 33
error
Printing the Content of a Object
Often you want to see the content of objects
class Person :
name = "Default Name "
year_born = 1996
print(p1)
print(p2)
class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def __str__(self):
return '(' + str(self.x) + ' ' + str(self.y) + ' ' + str(self.z) + ')'
class Sphere :
def __init__(self, center, radius):
self.center = center
self.radius = radius
def __str__(self):
return "Center : " + str(self.center) + " , Radius : " + str(self.radius)
center = Vector3(1,2,3)
sphere = Sphere(center, 10)
print(sphere)
Center : (1 2 3) , Radius : 10
built-in
Lecture 08
Class II – Inheritance
inheritance, name mangling, printing objects
The End