Creating new classes on the fly

Carlos Ribeiro carribeiro at gmail.com
Tue Oct 5 22:53:35 CEST 2004


I need to create new classes on the fly with different default
parameters, stored as class attributes. Of course, there's a reason
behind it: I need new classes, because I need to be able to build
multiple instances of them later. And I need new defaults, because the
values can't be provided at instantiation time (because of scoping &
mutability issues).

I've found two different ways to do it in the documentation:

new.classobj(name, baseclasses, dict)

and:

type(name, bases, dict)

I assume that both end up calling the same code, but I really don't
know which one am I supposed to call, in terms of being the most
'pythonic' way. Are both the same? Is one of them preferred over the
other?


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list