-
Notifications
You must be signed in to change notification settings - Fork 32
/
lec12prob2.py
79 lines (61 loc) · 1.6 KB
/
lec12prob2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# lec12prob2.py
#
# Lecture 12 - Object Oriented Programming
# Problem 2
#
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
# Python supports a limited form of multiple inheritance, demonstrated
# in the following code:
class A(object):
def __init__(self):
self.a = 1
def x(self):
print "A.x"
def y(self):
print "A.y"
def z(self):
print "A.z"
class B(A):
def __init__(self):
A.__init__(self)
self.a = 2
self.b = 3
def y(self):
print "B.y"
def z(self):
print "B.z"
class C(object):
def __init__(self):
self.a = 4
self.c = 5
def y(self):
print "C.y"
def z(self):
print "C.z"
class D(C, B):
def __init__(self):
C.__init__(self)
B.__init__(self)
self.d = 6
def z(self):
print "D.z"
'''
Which __init__ methods are invoked and in which order is determined by the
coding of the individual __init__ methods.
When resolving a reference to an attribute of an object that's an instance
of class D, Python first searches the object's instance variables then uses a
simple left-to-right, depth first search through the class hierarchy. In this
case that would mean searching the class C, followed the class B and its
superclasses (ie, class A, and then any superclasses it may have, et cetera).
With the definitions above if we define
obj = D()
then what is printed by each of the following statements?
print obj.a
print obj.b
print obj.c
print obj.d
obj.x()
obj.y()
obj.z()
'''