If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
Chris Rebert
clp2 at rebertia.com
Thu Oct 29 19:15:38 EDT 2009
On Thu, Oct 29, 2009 at 3:45 PM, metal <metal29a at gmail.com> wrote:
> Consider the following:
<snip>
> class Parent:
> def some_method(self):
> return Parent(...)
> class Child:
> pass
> ########################################
>
> Child().some_method() returns a Parent instance.
>
> We can rewrite Parent like this to avoid that
>
> ########################################
> class Parent:
> def some_method(self):
> return self.__class__(...)
> class Child:
> def some_method(self):
> ...
> return Parent.some_method(self)
> ########################################
>
> But this style makes code full with ugly self.__class__
>
> Any standard/pythonic way out there?
That pretty much is the standard way AFAIK. A few underscores aren't
all that bad in the grand scheme of things.
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list