Specifying __slots__ in a dynamically generated type

Steven Bethard steven.bethard at gmail.com
Sun Mar 27 21:52:46 EST 2005


Ron Garret wrote:
> In article <K-idnZPlpO4ZttrfRVn-2g at comcast.com>,
>  Steven Bethard <steven.bethard at 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



More information about the Python-list mailing list