Initializing a subclass with a super object?

F

frankdmartinez

Class A inherits from class B. Can anyone point me in the direction
of documentation saying how to initialize an object of A, a1, with an
object of B, b1?
 
T

Terry

Class A inherits from class B. Can anyone point me in the direction
of documentation saying how to initialize an object of A, a1, with an
object of B, b1?

This is called a 'factory'in design patterns. Search 'python factory',
you will get a lot of examples.

br, Terry
 
F

frankdmartinez

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.
 
F

frankdmartinez

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)

Hi, Francesco.
Thanx! That's actually exactly what I needed (though I didn't
know it).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top