-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass_access_parent_members.py
56 lines (41 loc) · 1.07 KB
/
class_access_parent_members.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
# Date: 2018-09-04
#
# Description:
# Access methods of parents class in derived class.
# Usage of super() in python3.
class Base1():
def __init__(self, x):
self.x = x
class Derived1(Base1):
def __init__(self, x, y):
Base1.x = x
self.y = y
def printXY(self):
print (Base1.x, self.y)
d = Derived1(10, 20)
d.printXY()
print ('\nAnother example, usage of super()')
class Base2():
def __init__(self, x):
self.x = x
class Derived2(Base2):
def __init__(self, x, y):
# We can assign 'x' using class name also, as done above.
# Base2.x = x
# This also works.
# Base2.__init__(self, x)
print ('Calling parent class __init__ using super()')
super(Derived2, self).__init__(x) # Works in python3 and above
self.y = y
def printXY(self):
print (self.x, self.y)
d = Derived2(50, 100)
d.printXY()
# Output:
# ----------------------------------------
# 10 20
#
# Another example, usage of super()
# Calling parent class __init__ using super()
# 50 100