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 7 1400
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
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
[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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: simon place |
last post by:
Removing __slots__
~~~~~~~~~~~~~~~~~~~
To do this nicely requires the renaming of __dict__ to, say,
__attribs__ , ( since dict is unnecessarily unspecific, this seem like a
small improvement...
|
by: flori |
last post by:
i try to greate somthing like this
class ca(object): __slots__ = ("a",)
class cb(ca): __slots__ = ("a","b")
class cc(ca): __slots__ = ("a","c")
class cd(cb,cc): __slots__ = ("a","b","c","d")
...
|
by: Nick Jacobson |
last post by:
The __slots__ attribute of new-style classes can reduce memory usage
when there are millions of instantiations of a class.
So would a __slots__ attribute for functions/methods have a similar...
|
by: Porky Pig Jr |
last post by:
Hello, I"m still learning Python, but going through the Ch 5 OOP of
Nutshell book. There is discussion on __slots__, and my understanding
from reading this section is that if I have a class...
|
by: Ewald R. de Wit |
last post by:
I'm running into a something unexpected for a new-style class
that has both a class attribute and __slots__ defined. If the
name of the class attribute also exists in __slots__, Python
throws an...
|
by: dan.j.weber |
last post by:
I see some programs declaring the names of class variables in
"__slots__". I've looked this up and the docs say something about old
and new style classes, whatever that means. Can someone give me a...
|
by: verbatim |
last post by:
with the following page, the dynamically generated fields are not recognized when i try to submit the form, or add more elements.
when i hit my submit button, the address bar has only x1 - x5 in...
|
by: Srikanth Ram |
last post by:
Hi,
I'm creating a PHP application. In this a dynamic table with the fields in the database is generated in a page. I have placed a checkbox in each row of the table to approve/disapprove...
|
by: Licheng Fang |
last post by:
Python is supposed to be readable, but after programming in Python for
a while I find my Python programs can be more obfuscated than their C/C
++ counterparts sometimes. Part of the reason is that...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |