TL; DR
Python . , . , . , , , . Python , , ; .
, Python . , Python , . Python , .
Base2, . Base1 Base2 object. ( , , .) print Base2:
class Base2(object):
def __init__(self, x2 , y2):
print type(self)
print id(self)
self.a2=x2
self.b2=y2
self.c2=self.multiply(self.a1,self.b1)
def multiply(self, p,q):
return p*q
:
>>> d = Derived()
<class '__main__.Derived'>
42223600
>>> print id(d)
42223600
, , Base2 Python , self Derived. Python , , self a1 b1; . , . , . , Base2 :
>>> Base2(1, 2)
<class '__main__.Base2'>
41403888
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in __init__
AttributeError: 'Base2' object has no attribute 'a1'
, print, a1. Python , .
:
>>> b = Base1(1,2)
>>> b.a1
1
>>> b.notyet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Base1' object has no attribute 'notyet'
>>> b.notyet = 'adding an attribute'
>>> b.notyet
'adding an attribute'
Base2 Base1. , - Derived, , - Base2 , Base2, Base1. , , . Python . , , . , Python , , . : .
, Base2 multiply. c2 Derived, multiply, a1 b1.
property:
class Derived(Base1,Base2):
def __init__(self):
self.describe='Derived Class'
Base1.__init__(self,3,4)
Base2.__init__(self,5,6)
@property
def c2(self):
return self.multiply(self.a1,self.b1)
( ) , . " ", :
x = Derived()
print x.c2
12, .