Creating new classes on the fly

brian brian at brianandkate.com
Mon Oct 11 19:06:23 EDT 2004


On 2004-10-05 15:53:35 -0500, Carlos Ribeiro <carribeiro at gmail.com> said:

> 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?

What about using a factory method to do this for you (or have I once 
again misinterpeted the question).

Brian




More information about the Python-list mailing list