return same type of object

David Isaac aisaac0 at verizon.net
Tue Oct 24 13:57:02 EDT 2006


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





More information about the Python-list mailing list