Initializing a subclass with a super object?

Francesco Bochicchio bockman at virgilio.it
Sun May 11 03:19:22 EDT 2008


On Sat, 10 May 2008 18:09:02 -0700, frankdmartinez wrote:

> Hi, Terry.
>     Yeah, no.  If we think of the inherited B as an object nested
> within a1, I'm attempting to initialize that B with b1 by accessing
> the B, say, via a function call.  I don't see how using a python
> factory achieves this.

But there is not such a thing, in Python. What you have is that A
has the same attributes/methods of B plus its own. 
What you could do is  adding in class A a method like this:

  class A(B):
     ...
     def set_b_attributes(self, instance_of_b):
         for k, value in instance_of_b.__dict__:
		setattr(self, k, value )

and the use it like this:

   a1.set_b_attributes(b1)

Of course, if b attributes are few and always the same it is more
readable assigning them explicitely:

     def set_b_attributes(self, instance_of_b):
         self.attr1 = instance_of_b.attr1
	 self.attr2 = instance_of_b.attr2
         ....

Ciao
-----
FB



       



More information about the Python-list mailing list