How to implement refactoring Parameter Objects in Python?

Right now I am using the parameter object class for inheritance like this:

class A():
    def __init__(self,p1,p2):
        self.p1, self.p2 = p1, p2

class B(A):
    def __init__(self,b):
        self.p1, self.p2 = b.p1, b.p2

This reduces the absurdity of using code, but not the class code itself. So, I would like to make a C ++ object and pass the parameter object to the initialization list as follows

class A { 
    int p1,p2; 
}
class B : public A { 
    B(const A& a) : A(a) {} 
}

Can I do this in Python? In particular, can I set the attributes of the parent class by somehow calling it __init__inside the child? - from reading "Immersion in Python", I would suggest that I can do this, since the object is already built at the time of the call __init__.

Or maybe there is some other way to implement a parameter object reparator in Python (and I'm just trying to force a C ++ technique)?

+3
3

, , , . Python , , .

, , , ++. , . - , Python .

Python, - , , , "" .

, , , , , , Python.

+3

++ python :

class A():
    def __init__(self,p1,p2):
        self.p1, self.p2 = p1, p2

class B(A):
    def __init__(self,b):
        A.__init__(self, b.p1, b.p2)
+2
class B(A):
    def __init__(self, *args, **kwargs):
        super(B, self).__init__(*args, **kwargs)

Read more about super()for details.

+1
source

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


All Articles