creating a similar object from an derived class

Discussion in 'Python' started by Bruno Desthuilliers, Sep 3, 2008.

  1. Scott a écrit :
    > Let's say I have an object:


    s/object/class/

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


    class Foo(object):
    def create_another(self):
    return Foo()

    > def blah():


    def blah(self):
    > x = self.create_another()
    > ... do something with X


    > Now I create a inherited class of this object:
    >
    > class bar(foo):


    class Bar(Foo):
    > ...
    >
    > If I call bar.create_another(), it will


    Actually, it will raise a TypeError...

    > return a foo() instead of a
    > bar(). This isn't what I want. I would like bar.create_another() to
    > create an instance for bar().


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


    And while you're at it, since - at least in this concrete case - you
    need access to the class but not to the instance, you could make it a
    classmethod:

    class Foo(object):
    @classmethod
    def create_another(cls):
    return cls()


    HTH
     
    Bruno Desthuilliers, Sep 3, 2008
    #1
    1. Advertising

  2. Bruno Desthuilliers

    Scott Guest

    Let's say I have an object:

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

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

    Now I create a inherited class of this object:

    class bar(foo):
    ...

    If I call bar.create_another(), it will return a foo() instead of a
    bar(). This isn't what I want. I would like bar.create_another() 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
     
    Scott, Sep 3, 2008
    #2
    1. Advertising

  3. Bruno Desthuilliers

    Matimus Guest

    On Sep 3, 12:09 pm, Scott <> wrote:
    > Let's say I have an object:
    >
    > class foo():
    >    def create_another()
    >        return foo()
    >
    >    def blah():
    >        x = self.create_another()
    >        ... do something with X
    >
    > Now I create a inherited class of this object:
    >
    > class bar(foo):
    >     ...
    >
    > If I call bar.create_another(), it will return a foo() instead of a
    > bar(). This isn't what I want. I would like bar.create_another() 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_another()
    >>> isinstance(e, D)

    True


    Matt
     
    Matimus, Sep 3, 2008
    #3
  4. Bruno Desthuilliers

    MRAB Guest

    On Sep 3, 8:09 pm, Scott <> wrote:
    > Let's say I have an object:
    >
    > class foo():
    >    def create_another()
    >        return foo()
    >
    >    def blah():
    >        x = self.create_another()
    >        ... do something with X
    >
    > Now I create a inherited class of this object:
    >
    > class bar(foo):
    >     ...
    >
    > If I call bar.create_another(), it will return a foo() instead of a
    > bar(). This isn't what I want. I would like bar.create_another() 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
     
    MRAB, Sep 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    596
    justanotherguy
    Dec 3, 2004
  2. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    412
    Victor Bazarov
    May 23, 2007
  4. David
    Replies:
    3
    Views:
    435
    Grizlyk
    Jan 29, 2008
  5. DanielJohnson
    Replies:
    7
    Views:
    2,043
    James Kanze
    Jan 17, 2009
Loading...

Share This Page