Inherit Class Dynamicly

Oren Tirosh oren-py-l at hishome.net
Tue Feb 5 10:29:35 EST 2002


On Tue, Feb 05, 2002 at 12:49:56PM +0000, Xiao-Qin Xia wrote:
> Hi, everybody,
> 
> from Tkinter import *
> class NewWidget:
>         ...
>         def __init__(self, widget_name = "Button", **dict):
>                 # What is needed here to make the new born object(self) 
>                 # behavior like both the class specified by widget_name
>                 # and NewWidget?
> 

def NewWidget(widget_name, **dict):

    class DynamicallyGenerated(FixedParentClass, widget_classes[widget_name]):
       ...

    return DynamicallyGenerated(**dict)


A function returning a dynamically generated class is quite similar to a
C++ template.  It's probably a good idea to memoize such dynamically 
generated classes - creating a new class for each widget is rather
expensive.

	Oren





More information about the Python-list mailing list