views:

72

answers:

3

Right now I use the parameter object's class to be inherited like so

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 trims up the absurdity of using the code but not the class code itself. So, I'd like to do the C++ thing and pass the parameter object to the initialization list like so

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

Can I do this in Python? Specifically, can I set the attributes of a parent class by somehow calling it's __init__ within the child's? - from reading "Dive into Python" I'd guess I can do this since the object is already constructed by the time __init__ is called.

Or, perhaps is there some different method of implementing the parameter object refactor in Python (and I'm just trying to force a C++ technique)?

+1  A: 
class B(A):
    def __init__(self, *args, **kwargs):
        super(B, self).__init__(*args, **kwargs)

Read more about super() for details.

Fragsworth
If that's all you were going to do in the `B.__init__` you could just leave it out entirely though. The parent `__init__` would do the job for you.
Peter Hansen
Hmm.... on second reading, this isn't even correct! It doesn't transfer any attributes from another instance, as the OP appears to want.
Peter Hansen
I think the important thing about the original post is that the single argument passed to the subclass can be unpacked to something useful by the base class, and the super() approach (and thus this answer) is definitely one solution to this.
Kylotan
@Kylotan, sorry, I don't buy it. The existing base class does not already unpack a single argument, and having to change it to do so completely defeats the whole idea of inheritance and where your subclasses modify or extend base class behaviour.
Peter Hansen
+3  A: 

Generally speaking, you likely don't need the parameter object refactor technique. Python has several collections in the standard library that already have the core benefits that a parameter object refactor would provide.

Additionally, the cost/benefit analysis for a refactor like the parameter object design pattern is notably different than C++. For instance, without static compilation, object creation and member lookups are much more expensive. The base case of the parameter object pattern is to provide a namespace for related fields, something that can be accomplished simply enough in Python with a dictionary.

The pattern has more value in Python if you need some kind of model manipulation in the form of methods that are related to the grouped data, but even then mechanisms as simple and inexpensive as "properties" can solve manipulation needs on member attributes.

With a slightly more detailed description of the problem space, we can probably help evaluate whether this sort of refactor is a benefit, but most likely you'd be better not using that design pattern when working in Python.

Travis Bradshaw
Yes, I omitted the conceptual issues that also drove my choosing to use a parameter object. Right now I'm just translating this code from C++ into Python - I'll have to think about this more clearly later.
bias
+2  A: 

The translation of the C++ code into python would be something like:

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)
quamrana