Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

creating a similar object from an derived class

 
Jump to:  
 
Scott
PostPosted: Wed Sep 03, 2008 7:09 pm    Post subject: 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_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
 

 
Bruno Desthuilliers
PostPosted: Wed Sep 03, 2008 7:09 pm    Post subject: Re: creating a similar object from an derived class
       
Scott a écrit :
Quote:
Let's say I have an object:

s/object/class/

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

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

Quote:
def blah():

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

Now I create a inherited class of this object:

class bar(foo):

class Bar(Foo):
Quote:
...

If I call bar.create_another(), it will

Actually, it will raise a TypeError...

Quote:
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
 

 
Matimus
PostPosted: Wed Sep 03, 2008 7:51 pm    Post subject: Re: creating a similar object from an derived class
       
On Sep 3, 12:09 pm, Scott <smba...@gmail.com> wrote:
Quote:
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:

Quote:
class C(object):
.... @classmethod

.... def create_another(cls):
.... return cls()
....
Quote:
class D(C):
.... pass

....
Quote:
d = D()
e = d.create_another()
isinstance(e, D)
True



Matt
 

 
MRAB
PostPosted: Wed Sep 03, 2008 10:29 pm    Post subject: Re: creating a similar object from an derived class
       
On Sep 3, 8:09 pm, Scott <smba...@gmail.com> wrote:
Quote:
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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Konferencje Szklarska Poręba dom development krzesła nowy styl Interactive agency Ogrody zimowe - rola