[Tutor] Dynamic inheritance?
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sun Nov 20 00:23:02 CET 2005
On Sat, 19 Nov 2005, Jan Eden wrote:
> I have a number of classes, each of which should inherit from a related
> superclass in another module:
>
> class A(Super.A):
> ...
>
> class B(Super.B):
> ...
>
> class C(Super.C):
> ...
>
> Is there a way to dynamically determine the value of Super at runtime?
Here's a small example that shows how classes can be treated just like any
other value in Python:
#########################################
def makeYa(superclass):
class Ya(superclass):
def sayHi(self):
superclass.sayHi(self)
print "ya"
return Ya
class ValleyPerson:
def sayHi(self):
print "like, so, oh my gosh, hi?"
FrankensteinClass = makeYa(ValleyPerson)
instance = FrankensteinClass()
instance.sayHi()
#########################################
So there's no need to do trickery with conditional imports; Python treats
classes as first-class objects that can be passed around.
Best of wishes!
More information about the Tutor
mailing list