Simple class inheritance in Python

I am new to Python and OOP. Suppose I have two classes: Base1and Base2. Suppose also that he Base1calculated some values a1, b1and that he Base2has a method that multiplies two values. My question is: what is the correct way to transfer a1and b1of Base1to multiply by Base2?

One way to do this is by defining a derived class Derivedas follows:

class Base1:
    def __init__(self, x1 , y1):
        # perform some computation on x1 and y1
        # store result in a1 and b1
        self.a1=x1
        self.b1=y1

class Base2:
    def __init__(self, x2 , y2):
        self.a2=x2
        self.b2=y2
        self.c2=self.multiply(self.a1,self.b1) # using a1 and b1 of Base1

    def multiply(self, p,q):
        return p*q

class Derived(Base1,Base2):
    def __init__(self):
        self.describe='Derived Class'
        Base1.__init__(self,3,4)
        Base2.__init__(self,5,6)

Then:

f=Derived()
f.c2=12

However, in a more difficult situation it is easy to lose information about where you came from self.a1, self.b1. It is also not obvious why the two base classes can access each other's attributes and methods in this way?

Edit: this is Python 2.7.10.

+4
3

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) # using a1 and b1 of Base1

    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) # using a1 and b1 of Base1

( ) , . " ", :

x = Derived()
print x.c2

12, .

+1

Python 2 object. , :

class Base1(object):
    def __init__(self, x1 , y1):
        # perform some computation on x1 and y1
        # store result in a1 and b1
        self.a1 = x1
        self.b1 = y1

class Base2(object):
    def __init__(self, x2 , y2):
        self.a2 = x2
        self.b2 = y2
        self.c2 = self.multiply(self.a1, self.b1) # using a1 and b1 of Base1

    def multiply(self, p,q):
        return p*q

class Derived(Base1, Base2):
    def __init__(self):
        self.describe='Derived Class'
        Base1.__init__(self, 3, 4)
        Base2.__init__(self, 5, 6)

Python , (mro). :

>>> Derived.mro()
[__main__.Derived, __main__.Base1, __main__.Base2, object]

, Python multiply() Derived. , . , mro, . Base1 Base2 Derived(Base1,Base2) , mro:

class Derived2(Base2, Base1):
    pass


>>> Derived2.mro()
[__main__.Derived2, __main__.Base2, __main__.Base1, object]

self . f (f = Derived()). , f . self.x = something .

+2

multiply , , a1 b1 . ,

class Base1(object):
    def __init__(self,a1,b1):
        self.a1 = a1
        self.b1 = b1

class Base2(Base1):

    def multiply():
        return self.a1*self.b1

Here, when you give __init__to base2, it will use the init base1 method that takes parameters like a1anda2

so now

base = Base2(5,4)
print(base.multiply())
0
source

Source: https://habr.com/ru/post/1629675/


All Articles