how do I make a class global?
harold
dadapapa at googlemail.com
Thu Apr 27 13:23:27 EDT 2006
basically, you can create new types on the fly using type() with three
arguments:
my_class = type("className",(BaseClass,),class_dict)
then, you can assign this vlass to the golbal namespace using
globals():
globals()["className"] = my_class
In your case, you would need to populate the class_dict by a function
object
that you parameterize to your needs, e.g.
def create_class(name,params) :
def cls_init(self) :
BaseClass.__init__(self)
self.params = params
cls_dict = {
"__init__" : cls_init
}
new_cls = type(name,(BaseClass,),cls_dict)
globals()[name] = new_cls
the result would be like this:
>>> create_class("test_class",42)
>>> instance = test_class()
>>> print instance.params
42
More information about the Python-list
mailing list