By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Specifying __slots__ in a dynamically generated type

P: n/a

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
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ron Garret wrote:

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.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
Ron Garret wrote:
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
Jul 18 '05 #3

P: n/a
[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'.

--
François Pinard http://pinard.progiciels-bpi.ca
Jul 18 '05 #4

P: n/a
Ron Garret wrote:
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.
FooClass = type('foo', (object, ), {'__slots__': 'foo'})
foo = FooClass()
foo.bar = 1 Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'foo' object has no attribute 'bar' foo.foo = 2

Jul 18 '05 #5

P: n/a
In article <rp*****************@newshog.newsread.com>,
Leif K-Brooks <eu*****@ecritters.biz> wrote:
Ron Garret wrote:
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.
>>> FooClass = type('foo', (object, ), {'__slots__': 'foo'})
>>> foo = FooClass()
>>> foo.bar = 1 Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'foo' object has no attribute 'bar' >>> foo.foo = 2


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
Jul 18 '05 #6

P: n/a
In article <K-********************@comcast.com>,
Steven Bethard <st************@gmail.com> wrote:
Ron Garret wrote:
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


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
Jul 18 '05 #7

P: n/a
Ron Garret wrote:
In article <K-********************@comcast.com>,
Steven Bethard <st************@gmail.com> wrote:
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.


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
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.