472,364 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

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 1127
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
2
by: philjhanna | last post by:
Hi, Does anyone know why I can't add return false with addEventListener in firefox (1.0.6). This demonstrates the problem If return false is added to onmousedown then you can drag over the...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
1
by: Thomas D. | last post by:
Hello all, I'm using the IXmlSerializable interface for a project and encounter some problems when testing my webservice in a client application. I know this interface is undocumented and not...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: 6kjfsyg02 | last post by:
I am trying to return one of two different objects from the same method, but I can not do it in WSDL or C#. I have a web service with three methods. I have been told that one of the methods...
1
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
9
by: Alexander Widera | last post by:
hi, is it possible to return an object of an unknown (but not really unknown) type with an method? i have the following situation: - a variable (A) of the type "object" which contains the...
2
by: Simon Woods | last post by:
Hi I am storing data type information in a config file as a string. I think the queation I want to ask is "Is it possible to convert it into a Generic Type". So, e.g., I may have a selection...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.