469,362 Members | 2,441 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

return same type of object

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
Oct 24 '06 #1
6 1063
David Isaac wrote:
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...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 24 '06 #2
David Isaac wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 24 '06 #3

Bruno wrote:
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
Oct 24 '06 #4
David Isaac a écrit :
Bruno wrote:
>>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.
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.
Oct 24 '06 #5
Bruno Desthuilliers wrote:
David Isaac a écrit :
[...]
>
>>But Steve suggests going with the latter.


That's what I'd do too a priori.
Believe it or not, I wasn't at the priory when I wrote that.

Sorry.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 24 '06 #6
Steve Holden wrote:
Bruno Desthuilliers wrote:
>David Isaac a écrit :
[...]
>>
>>But Steve suggests going with the latter.


That's what I'd do too a priori.
Believe it or not, I wasn't at the priory when I wrote that.
Steve, if it's a pun, I'm afraid I just don't get it...
Sorry.
<aol />

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 25 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by DaKoadMunky | last post: by
12 posts views Thread by Michael Maes | last post: by
2 posts views Thread by 6kjfsyg02 | last post: by
32 posts views Thread by zl2k | last post: by
9 posts views Thread by Alexander Widera | last post: by
2 posts views Thread by Simon Woods | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.