If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- From: metal <metal29a@xxxxxxxxx>
- Date: Thu, 29 Oct 2009 15:45:45 -0700 (PDT)
Consider the following:
########################################
class Parent:
def some_method(self):
return Parent(...)
class Child:
def some_method(self):
...
return Parent.some_method(self)
########################################
Or Simply:
########################################
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?
.
- Follow-Ups:
- Re: If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- From: Bruno Desthuilliers
- Re: If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- From: metal
- Re: If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- From: Rhodri James
- Re: If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- From: Chris Rebert
- Re: If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- From: Christian Heimes
- Re: If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- Prev by Date: Re: PyQT4 user group
- Next by Date: Re: How to get a unique function name for methods
- Previous by thread: Aaaargh! "global name 'eggz' is not defined"
- Next by thread: Re: If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?
- Index(es):
Relevant Pages
|