Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > creating a similar object from an derived class

Reply
Thread Tools

creating a similar object from an derived class

 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-03-2008
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
 
Reply With Quote
 
 
 
 
Scott
Guest
Posts: n/a
 
      09-03-2008
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


 
Reply With Quote
 
 
 
 
Matimus
Guest
Posts: n/a
 
      09-03-2008
On Sep 3, 12:09*pm, Scott <smba...@gmail.com> 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
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      09-03-2008
On Sep 3, 8:09*pm, Scott <smba...@gmail.com> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why should base class object points to derived class object (virtualfunction question) ? DanielJohnson C++ 7 01-17-2009 10:15 PM
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57