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 in itself. ) then using the setting of __attribs__ to
a non-mutable type (ie tuple of attribute names) to indicate the
behaviour of __slots__, rendering it unnecessary, this I think is a good
simplification. 6 1964
The point is to combine __dict__ and __slots__ into a new __attribs__,
the distinction being the type of __attribs__. If you don't specify
__attribs__ in the class you get the default __dict__ behavior, if you
do, and use a tuple, then you get the __slots__ behavior, and you can
easily tell which by checking the type, you could also iterate over the
attributes without caring which it was.
simon place <si*********@whsmithnet.co.uk> writes: The point is to combine __dict__ and __slots__ into a new __attribs__, the distinction being the type of __attribs__. If you don't specify __attribs__ in the class you get the default __dict__ behavior, if you do, and use a tuple, then you get the __slots__ behavior, and you can easily tell which by checking the type, you could also iterate over the attributes without caring which it was.
So how is this different from the current situation? If you don't
specify __slots__, you get the default __dict__ behaviour, if you do,
and use a tuple, you get the __slots__ behaviour, and you can easily
tell which by checking the type.
You also have the case of both __slots__ and __dict__ being in a type,
and this is a useful case also.
Regards,
Martin
i can't think of a point for __slots__ except to save the overhead of a
dict, this is why you DON'T HAVE a __dict__ when __slots__ is defined.
__slots__ should generally be used to improve the performance/footprint
of small/transient classes, ( it also prevents new instance variables
but this appears to be more of a side effect.)
The point of the combining is to simplify, you know, based on the idea
that keeping the language simply ( and logical ) aids comprehension.
simon place <si*********@whsmithnet.co.uk> writes: i can't think of a point for __slots__ except to save the overhead of a dict, this is why you DON'T HAVE a __dict__ when __slots__ is defined.
No. Some classes have slots for efficiency, and their subclasses have
dictionaries for generality.
Likewise, some classes have slots to save the dictionary for most
instances, but some instances may need additional attributes, in which
case Python creates the dictionary on-the-fly.
The point of the combining is to simplify, you know, based on the idea that keeping the language simply ( and logical ) aids comprehension.
I know.
Regards,
Martin
> No. Some classes have slots for efficiency, and their subclasses have dictionaries for generality.
Likewise, some classes have slots to save the dictionary for most instances, but some instances may need additional attributes, in which case Python creates the dictionary on-the-fly.
I know subclasses can add a __dict__, but i really thought a class with
__slots__ could not have a __dict__, doesn't the script below show this
behavior?
PythonWin 2.3b2 (#43, Jun 29 2003, 16:43:04) [MSC v.1200 32 bit (Intel)]
on win32.
Portions Copyright 1994-2001 Mark Hammond (mh******@skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information. class A(object):
.... __slots__=['a']
.... b=A() b
<__main__.A object at 0x00EFABF0> b.__dict__
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'A' object has no attribute '__dict__' b.a
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: a b.a=1 b.a
1 b.b=1
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'A' object has no attribute 'b'
"simon place" <si*********@whsmithnet.co.uk> schrieb im Newsbeitrag
news:3f**********@news1.vip.uk.com... No. Some classes have slots for efficiency, and their subclasses have dictionaries for generality.
Likewise, some classes have slots to save the dictionary for most instances, but some instances may need additional attributes, in which case Python creates the dictionary on-the-fly.
I know subclasses can add a __dict__, but i really thought a class with __slots__ could not have a __dict__, doesn't the script below show this behavior?
<snip>
Consider this:
C:\Dokumente und Einstellungen\Administrator>python
Python 2.3a2 (#39, Feb 19 2003, 17:58:58) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information. class blah(object):
.... __slots__ = ['a','__dict__']
.... a = blah() a.__dict__
{} a.b = 5
which of course is kina wierd....
Ciao Ulrich This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by anabell |
last post: by
|
9 posts
views
Thread by flori |
last post: by
|
5 posts
views
Thread by Jean Brouwers |
last post: by
|
3 posts
views
Thread by Nick Jacobson |
last post: by
|
7 posts
views
Thread by Porky Pig Jr |
last post: by
|
2 posts
views
Thread by Ewald R. de Wit |
last post: by
|
3 posts
views
Thread by Schüle Daniel |
last post: by
|
1 post
views
Thread by pascal.parent |
last post: by
|
27 posts
views
Thread by Licheng Fang |
last post: by
| | | | | | | | | | |