If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?

metal metal29a at gmail.com
Thu Oct 29 18:45:45 EDT 2009


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?




More information about the Python-list mailing list