return same type of object

D

David Isaac

Instances of MyClass have a method that
returns another instance. Ignoring the details
of why I might wish to do this, I could
return MyClass()
or
return self.__class__()

I like that latter better. Should I?
Should I do something else altogether?

Thanks,
Alan Isaac
 
B

Bruno Desthuilliers

David said:
Instances of MyClass have a method that
returns another instance.

This is usually known as a 'factory method'.
Ignoring the details
of why I might wish to do this, I could
return MyClass()
or
return self.__class__()

I like that latter better. Should I?

You do realise that both solutions are *not* strictky equilavent, do you?

class PsychoRigid(object):
def create(self):
return PsychoRigid()

class PsychoRigidChild(PsychoRigid):
pass

pr1 = PsychoRigidChild()
pr2 = pr1.create()
print "pr2 is a ", type(pr2)

class Virtual(object):
def create(self):
return self.__class__()

class VirtualChild(Virtual):
pass

vc1 = VirtualChild()
vc2 = vc1.create()
print "vc2 is a ", type(vc2)


My 2 cents...
 
S

Steve Holden

David said:
Instances of MyClass have a method that
returns another instance. Ignoring the details
of why I might wish to do this, I could
return MyClass()
or
return self.__class__()

I like that latter better. Should I?
Should I do something else altogether?
The latter solution is more Pythonic, IMHO, as it works for subclasses.

regards
Steve
 
D

David Isaac

Bruno said:
This is usually known as a 'factory method'. You do realise that both
solutions are *not* strictky equilavent, do you?

Your point I believe is that after inheritance the factory method
in the subclass will still
return MyClass()
but will return an instance of the subclass if I
return self.__class__()

Right.

You did not comment further so I take it your view is that
each is fine, pick the one that gives the behavior you want.
But Steve suggests going with the latter.
Here is an (very crude) argument for going with the latter:
if you know you want an instance of MyClass(),
you can do that directly, but if you do this via a factory method,
then (after inheritance) the action of the factory method becomes obscured.

Does that make any sense?

Thanks,
Alan Isaac
of the factory
 
B

Bruno Desthuilliers

David Isaac a écrit :
solutions are *not* strictky equilavent, do you?

Your point I believe is that after inheritance the factory method
in the subclass will still
return MyClass()
but will return an instance of the subclass if I
return self.__class__()

Right.
Right.

You did not comment further so I take it your view is that
each is fine, pick the one that gives the behavior you want.

I did not comment because I don't know which behaviour is appropriate in
*your* case.
But Steve suggests going with the latter.

That's what I'd do too a priori.
Here is an (very crude) argument for going with the latter:
if you know you want an instance of MyClass(),
you can do that directly,

Sure, but that's another point. If you want an instance of a subclass of
MyClass, you can also do that directly !-)

MVHO is that in most cases, one would expect such a factory method to
play nicely with subclassing. But as I said, this is not a hard rule.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top