Dynamic class construction?
David C. Ullrich
ullrich at math.okstate.edu
Sat Jan 29 10:40:39 EST 2000
Say I want to make a function that returns a new _class_.
I can do this:
def NewClass(whatever):
class new(baseClass):
pass
new.SomeAttribute = whatever.whatever
return new
and it seems to do the job. Is there another way to
do this that seems much better for some reason?
I guess a related question would be whether
having distinct classes with the same __name__
is going to be trouble - does __name__ get used
in significant ways internally or is it just there
for the programmer's convenience?
DU
(In case someone feels the answer might depend
on the context: it's mathematical structures. For
example baseClass might be Group; a subclass
of Group "is" a group, and an instance of the
subclass "is" an element of the group. So I might
want to create a group using
def NewGroup(groupOp):
class new(Group):
pass
new.__mul__ = groupOp
return new
so then
G = NewGroup(SomeGroupOp)
g1 = G(someparams)
makes g1 an element of the group G.)
More information about the Python-list
mailing list