Specifying __slots__ in a dynamically generated type

R

Ron Garret

I need to dynamically generate new types at run time. I can do this in
two ways. I can use the "type" constructor, or I can generate a "class"
statement as a string and feed that to the exec function. The former
technique is much cleaner all else being equal, but I want to be able to
specify the __slots__ class variable for these new types, and it seems
that to do that I need to use the latter method. Is that true? Is it
really impossible to specify __slots__ using the "type" constructor?

rg
 
D

Diez B. Roggisch

Ron said:
I need to dynamically generate new types at run time. I can do this in
two ways. I can use the "type" constructor, or I can generate a "class"
statement as a string and feed that to the exec function. The former
technique is much cleaner all else being equal, but I want to be able to
specify the __slots__ class variable for these new types, and it seems
that to do that I need to use the latter method. Is that true? Is it
really impossible to specify __slots__ using the "type" constructor?

This simple testscript


class Meta(type):
def __init__(*args):
print args
return type(*args[1:])

class Foo(object):
__metaclass__ = Meta
__slots__ = "foo", "bar"

Foo()


shows that __slots__ is just a part of the type's dict. So you can simply
specify it.
 
S

Steven Bethard

Ron said:
I need to dynamically generate new types at run time. I can do this in
two ways. I can use the "type" constructor, or I can generate a "class"
statement as a string and feed that to the exec function. The former
technique is much cleaner all else being equal, but I want to be able to
specify the __slots__ class variable for these new types, and it seems
that to do that I need to use the latter method. Is that true? Is it
really impossible to specify __slots__ using the "type" constructor?

Why don't you just write a function to create class objects?

def f(*params):
class C(...):
... # based on params
return C


STeVe
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Ron Garret]
Is it really impossible to specify __slots__ using the "type"
constructor?

It does not work? I vaguely remember having needed to do this once or
twice, and it worked immediatly as expected. Unless I remember wrongly,
you only have to preset `__slots__' in the dict you give to `type'.
 
L

Leif K-Brooks

Ron said:
I need to dynamically generate new types at run time. I can do this in
two ways. I can use the "type" constructor, or I can generate a "class"
statement as a string and feed that to the exec function. The former
technique is much cleaner all else being equal, but I want to be able to
specify the __slots__ class variable for these new types, and it seems
that to do that I need to use the latter method. Is that true? Is it
really impossible to specify __slots__ using the "type" constructor?

Using __slots__ with type() works for me:

Python 2.3.5 (#2, Feb 9 2005, 00:38:15)
[GCC 3.3.5 (Debian 1:3.3.5-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
 
R

Ron Garret

Leif K-Brooks said:
Ron said:
I need to dynamically generate new types at run time. I can do this in
two ways. I can use the "type" constructor, or I can generate a "class"
statement as a string and feed that to the exec function. The former
technique is much cleaner all else being equal, but I want to be able to
specify the __slots__ class variable for these new types, and it seems
that to do that I need to use the latter method. Is that true? Is it
really impossible to specify __slots__ using the "type" constructor?

Using __slots__ with type() works for me:

Python 2.3.5 (#2, Feb 9 2005, 00:38:15)
[GCC 3.3.5 (Debian 1:3.3.5-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):

Well, whaddya know. I don't know how I got the idea that this didn't
work. Maybe I left out an underscore when I tried it.

Thanks!

rg
 
R

Ron Garret

Steven Bethard said:
Why don't you just write a function to create class objects?

def f(*params):
class C(...):
... # based on params
return C

I suppose I could. When I originally started writing this code I wanted
each of the generated classes to have its own name, and I didn't realize
that you could accomplish this by assigning to cls.__name__ after you
created it. So I started down the road of using the type constructor.
But there's not really a good reason for it now. Maybe I'll go back and
change my code.

rg
 
S

Steven Bethard

Ron said:
I suppose I could. When I originally started writing this code I wanted
each of the generated classes to have its own name, and I didn't realize
that you could accomplish this by assigning to cls.__name__ after you
created it.

Yeah, that's what I'd do:

py> def f(name):
.... class C(object):
.... pass
.... C.__name__ = name
.... return C
....
py> f('D')
<class '__main__.D'>
py> f('Foo')
<class '__main__.Foo'>

STeVe
 

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

Forum statistics

Threads
474,228
Messages
2,571,157
Members
47,785
Latest member
deepusaini

Latest Threads

Top