Creating new classes on the fly

C

Carlos Ribeiro

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: (e-mail address removed)
mail: (e-mail address removed)
 
A

Alex Martelli

Carlos Ribeiro 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)

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.


Alex
 
C

Carlos Ribeiro

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.


This makes a new-style class (again, __metaclass__ in dict is ignored).


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
mean:

"""
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
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (e-mail address removed)
mail: (e-mail address removed)
 
A

Alex Martelli

Carlos Ribeiro said:
Question: why is it not pointed out clearly in the documentation? It

My guess is: because nobody ever submitted a documentation patch about
it.
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?

Yes, I agree with you, and I'm sure documentation patches dealing with
these problems would most likely be well received.


Alex
 
S

Steve Holden

Carlos Ribeiro wrote:

[...]
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?

Indeed there should, and you prove yourself well-qualified to write it!
Welcome to the world of open source ;-)

regards
Steve
 
B

brian

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top