a question on __slots__ (and example from Nutshell)

Porky Pig Jr porky_pig_jr at my-deja.com
Thu Jun 3 07:03:36 CEST 2004

Nop, a class Rectangle is defined as a new style class. Nuthsell, page
It is given as a part of discussion of 'properties', also part of new

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

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

>>> class OptimizedRectangle(Rectangle):   # subclass with __slots__
	__slots__ = 'width', 'heigth'

>>> optrec = OptimizedRectangle(3,4)
>>> optrec.area
>>> optrec.__dict__       # note that __dict__ shows up (why?)
>>> optrec.newarea = 12  # so I can define new attribute on a fly
>>> optrec.newarea
>>> optrec.__slots__     # slots are the part of instance 
('width', 'heigth')
>>> optrec.__dict__     # but so is the dict
{'newarea': 12}


More information about the Python-list mailing list