creating a similar object from an derived class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott

    creating a similar object from an derived class

    Let's say I have an object:

    class foo():
    def create_another( )
    return foo()

    def blah():
    x = self.create_ano ther()
    ... do something with X

    Now I create a inherited class of this object:

    class bar(foo):
    ...

    If I call bar.create_anot her(), it will return a foo() instead of a
    bar(). This isn't what I want. I would like bar.create_anot her() to
    create an instance for bar(). Obviously I can do this by overriding
    create_another, i.e.

    class bar(foo):
    def create_another( )
    return bar()

    However, is there a way for me to modify foo() so that it
    automatically creates objects of the derived class, so that I don't
    have to continue to redefine create_another( ) ?

    For example, I tried the following:

    def create_another( )
    return self.type()()

    but it did not work.

    Thanks,
    Scott


  • Matimus

    #2
    Re: creating a similar object from an derived class

    On Sep 3, 12:09 pm, Scott <smba...@gmail. comwrote:
    Let's say I have an object:
    >
    class foo():
       def create_another( )
           return foo()
    >
       def blah():
           x = self.create_ano ther()
           ... do something with X
    >
    Now I create a inherited class of this object:
    >
    class bar(foo):
        ...
    >
    If I call bar.create_anot her(), it will return a foo() instead of a
    bar(). This isn't what I want. I would like bar.create_anot her() to
    create an instance for bar(). Obviously I can do this by overriding
    create_another, i.e.
    >
    class bar(foo):
        def create_another( )
            return bar()
    >
    However, is there a way for me to modify foo() so that it
    automatically creates objects of the derived class, so that I don't
    have to continue to redefine create_another( ) ?
    >
    For example, I tried the following:
    >
    def create_another( )
        return self.type()()
    >
    but it did not work.
    >
    Thanks,
    Scott
    This works:
    >>class C(object):
    .... @classmethod
    .... def create_another( cls):
    .... return cls()
    ....
    >>class D(C):
    .... pass
    ....
    >>d = D()
    >>e = d.create_anothe r()
    >>isinstance( e, D)
    True


    Matt

    Comment

    • MRAB

      #3
      Re: creating a similar object from an derived class

      On Sep 3, 8:09 pm, Scott <smba...@gmail. comwrote:
      Let's say I have an object:
      >
      class foo():
         def create_another( )
             return foo()
      >
         def blah():
             x = self.create_ano ther()
             ... do something with X
      >
      Now I create a inherited class of this object:
      >
      class bar(foo):
          ...
      >
      If I call bar.create_anot her(), it will return a foo() instead of a
      bar(). This isn't what I want. I would like bar.create_anot her() to
      create an instance for bar(). Obviously I can do this by overriding
      create_another, i.e.
      >
      class bar(foo):
          def create_another( )
              return bar()
      >
      However, is there a way for me to modify foo() so that it
      automatically creates objects of the derived class, so that I don't
      have to continue to redefine create_another( ) ?
      >
      For example, I tried the following:
      >
      def create_another( )
          return self.type()()
      >
      but it did not work.
      >
      If you want a foo object to be able to create another foo object and a
      bar object to be able to create another bar object then you could do
      this:

      class foo():
      def create_another( self):
      return self.__class__( )

      class bar(foo):
      pass

      Comment

      Working...