a question on __slots__ (and example from Nutshell)

Ixokai usenet at ixokai.net
Thu Jun 3 09:43:32 CEST 2004


*blink* That's interesting.

I've never tried to add __slots__ to 'optimize' a parent like that. It
doesn't actually surprise me that a child inherits everything with its
parent-- including the dictionary since one is there. And if one is there,
it doesn't surprise me that you can assign arbitrary attributes to it.

Its nothing I was aware of-- but it doesn't surprise me. I wouldn't expect
having __slots__ there to go in and remove something that was present in a
parent class.

--Stephen


"Porky Pig Jr" <porky_pig_jr at my-deja.com> wrote in message
news:56cfb0e3.0406022103.211f06f8 at posting.google.com...
> Nop, a class Rectangle is defined as a new style class. Nuthsell, page
> 85.
> It is given as a part of discussion of 'properties', also part of new
> style
> classes.
>
> My apologies for refering to the parent class, rather than providing
> its complete definition. This is one of the golden rules when asking
> for support: provide complete list of session -- and I did not. Mea
> culpa.
>
> Let me start all over. Here is the complete IDLE session,
> except on a different machine with Python 2.3.3 rather than 2.3.4 but
> I assume this shouldn't reallhy matter:
>
>
>
> Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
> on win32
> Type "copyright", "credits" or "license()" for more information.
>
>     ****************************************************************
>     Personal firewall software may warn about the connection IDLE
>     makes to its subprocess using this computer's internal loopback
>     interface.  This connection is not visible on any external
>     interface and no data is sent to or received from the Internet.
>     ****************************************************************
>
> IDLE 1.0.2
> >>> class Rectangle(object):                    # defined as new class
> def __init__(self, width, heigth):
> self.width = width
> self.heigth = heigth
> def getArea(self):
> return self.width * self.heigth
> area = property(getArea, doc='area of the rectangle')
>
>
> >>> rect = Rectangle(4,5)
> >>> rect.area
> 20
>
> >>> class OptimizedRectangle(Rectangle):   # subclass with __slots__
> __slots__ = 'width', 'heigth'
>
>
> >>> optrec = OptimizedRectangle(3,4)
> >>> optrec.area
> 12
> >>> optrec.__dict__       # note that __dict__ shows up (why?)
> {}
> >>> optrec.newarea = 12  # so I can define new attribute on a fly
> >>> optrec.newarea
> 12
> >>>
> >>> optrec.__slots__     # slots are the part of instance
> ('width', 'heigth')
> >>>
> >>> optrec.__dict__     # but so is the dict
> {'newarea': 12}
> >>>
>
> TIA.





More information about the Python-list mailing list