Creating new classes on the fly

Carlos Ribeiro carribeiro at
Thu Oct 7 11:07:23 CEST 2004

On Thu, 7 Oct 2004 09:42:02 +0200, Alex Martelli <aleaxit at> wrote:
> Carlos Ribeiro <carribeiro at> wrote:
> > 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)
> This normally makes a 'classic class', unless some baseclasses are
> non-classic (e.g., object or other built-ins); note that __metaclass__
> in dict is ignored.
> >
> > and:
> >
> > type(name, bases, dict)
> This makes a new-style class (again,  __metaclass__ in dict is ignored).
> >
> > 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?
> They're not quite the same, if the bases tuple is empty or only has
> classic classes.  I would normally use the second form because new-style
> classes are generally preferred.

Question: why is it not pointed out clearly in the documentation? It
can be written better than this, but this is an example of what I

classobj(name, baseclasses, dict)
This function returns a new "old-style" class object, with name name,
derived from baseclasses (which should be a tuple of classes) and with
namespace dict.

New-style classes are built by the type builtin, or by any other type.
This is the preferred form since Python 2.(x), and use of classobj
should be avoided for new code.

btw -- the documentation is lacking at *many* points when it comes to
new style classes, metaclasses, descriptors, and the related
protocols. I couldn't find a clear description of the class creation
process there -- although there are lots of info in the Wiki... but
there *should* be something on the docs, don't you think?
Carlos Ribeiro
Consultoria em Projetos
mail: carribeiro at
mail: carribeiro at

More information about the Python-list mailing list