PEP idea. ( removing __slots__ )

Discussion in 'Python' started by simon place, Jul 5, 2003.

  1. simon place

    simon place Guest

    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.
     
    simon place, Jul 5, 2003
    #1
    1. Advertising

  2. simon place

    simon place Guest

    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, Jul 6, 2003
    #2
    1. Advertising

  3. simon place <> 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
     
    Martin v. =?iso-8859-15?q?L=F6wis?=, Jul 6, 2003
    #3
  4. simon place

    simon place Guest

    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, Jul 6, 2003
    #4
  5. simon place <> 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
     
    Martin v. =?iso-8859-15?q?L=F6wis?=, Jul 6, 2003
    #5
  6. simon place

    simon place Guest

    > 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 () -
    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, Jul 6, 2003
    #6
  7. simon place

    Ulrich Petri Guest

    "simon place" <> schrieb im Newsbeitrag
    news:3f08984c$...
    > > 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
     
    Ulrich Petri, Jul 7, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph Becker-Freyseng

    PEP for new modules (I read PEP 2)

    Christoph Becker-Freyseng, Jan 15, 2004, in forum: Python
    Replies:
    3
    Views:
    381
    Gerrit Holl
    Jan 16, 2004
  2. Andrew Koenig
    Replies:
    46
    Views:
    1,003
    Peter Maas
    Feb 24, 2004
  3. Chris Perkins
    Replies:
    3
    Views:
    307
    Scott David Daniels
    Mar 12, 2005
  4. Chris Perkins
    Replies:
    2
    Views:
    288
    Chris Perkins
    Mar 14, 2005
  5. Lie
    Replies:
    25
    Views:
    754
    Dafydd Hughes
    Dec 18, 2007
Loading...

Share This Page