Initializing a subclass with a super object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frankdmartinez@gmail.com

    Initializing a subclass with a super object?

    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?
  • Terry

    #2
    Re: Initializing a subclass with a super object?

    On May 11, 7:22 am, frankdmarti...@ gmail.com wrote:
    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

    Comment

    • frankdmartinez@gmail.com

      #3
      Re: Initializing a subclass with a super object?

      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.

      Comment

      • Francesco Bochicchio

        #4
        Re: Initializing a subclass with a super object?

        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_attribute s(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_attrib utes(b1)

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

        def set_b_attribute s(self, instance_of_b):
        self.attr1 = instance_of_b.a ttr1
        self.attr2 = instance_of_b.a ttr2
        ....

        Ciao
        -----
        FB




        Comment

        • frankdmartinez@gmail.com

          #5
          Re: Initializing a subclass with a super object?

          On May 11, 3:19 am, Francesco Bochicchio <bock...@virgil io.itwrote:
          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_attribute s(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_attrib utes(b1)
          Hi, Francesco.
          Thanx! That's actually exactly what I needed (though I didn't
          know it).

          Comment

          Working...