Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PEP idea. ( removing __slots__ )

Reply
Thread Tools

PEP idea. ( removing __slots__ )

 
 
simon place
Guest
Posts: n/a
 
      07-05-2003
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.

 
Reply With Quote
 
 
 
 
simon place
Guest
Posts: n/a
 
      07-06-2003
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.

 
Reply With Quote
 
 
 
 
Martin v. =?iso-8859-15?q?L=F6wis?=
Guest
Posts: n/a
 
      07-06-2003
simon place <(E-Mail Removed)> 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

 
Reply With Quote
 
simon place
Guest
Posts: n/a
 
      07-06-2003
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.

 
Reply With Quote
 
Martin v. =?iso-8859-15?q?L=F6wis?=
Guest
Posts: n/a
 
      07-06-2003
simon place <(E-Mail Removed)> 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
 
Reply With Quote
 
simon place
Guest
Posts: n/a
 
      07-06-2003
> 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 ((E-Mail Removed)) -
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'
>>>


 
Reply With Quote
 
Ulrich Petri
Guest
Posts: n/a
 
      07-07-2003
"simon place" <(E-Mail Removed)> schrieb im Newsbeitrag
news:3f08984c$(E-Mail Removed)...
> > 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:5 [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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About Rational Number (PEP 239/PEP 240) Lie Python 25 12-18-2007 06:13 AM
PEP for new modules (I read PEP 2) Christoph Becker-Freyseng Python 3 01-16-2004 04:26 PM
Metaclass and __slots__ anabell@sh163.net Python 1 11-06-2003 08:09 AM
question: usage of __slots__ T. Kaufmann Python 11 10-17-2003 09:18 PM
tp_base, tp_basesize, and __slots__ instance __class__ reassignment Jp Calderone Python 1 07-06-2003 07:17 AM



Advertisments