[Tutor] Dynamic inheritance?
Python
python at venix.com
Sat Nov 19 18:23:14 CET 2005
On Sat, 2005-11-19 at 16:45 +0100, Jan Eden wrote:
>
> Is there a way to dynamically determine the value of Super at runtime? Background: Depending on certain object attributes which are set during the object initialization, I need to use a different set of templates for the respective object.
>
If you use new style classes, then there is a super function that can be
used to automatically resolve a superclass reference. You can force new
style classes by:
inherit from object easy to see, but repetitive
__metaclass__ = type put before the class statements
super(Myclass,self).__init__(...)
will search through the inheritance tree and (in this case) invoke
__init__.
Use __bases__ to run up the inheritance yourself.
>>> class A:
... pass
...
>>> class B(A):
... pass
...
>>> b.__class__.__bases__
(<class __main__.A at 0xb7e9311c>,)
>>> B.__bases__
(<class __main__.A at 0xb7e9311c>,)
The "magic" class attributes don't get listed by the dir command so you
need to search the documentation to find this.
http://docs.python.org/lib/specialattrs.html
>>> dir(B)
['__doc__', '__module__']
>>> b = B()
>>> dir(b)
['__doc__', '__module__']
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list