creating a similar object from an derived class

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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
 
S

Scott

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
 
M

Matimus

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:
.... @classmethod
.... def create_another(cls):
.... return cls()
........ pass
....True


Matt
 
M

MRAB

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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top