a question on __slots__ (and example from Nutshell)

P

Porky Pig Jr

Hello, I"m still learning Python, but going through the Ch 5 OOP of
Nutshell book. There is discussion on __slots__, and my understanding
from reading this section is that if I have a class Rectangle (as
defined in some prior sections), and then I provide definition

class OptimizedRectangle(Rectangle):
__slots__ = 'width', 'heigth'

I can use the instance of OptimizedRectangle, say, x, with 'width' and
'heigth', but (quoting the book) 'any attempt to bind on x any
attribute whose name is not in C.__slots__ raises an exception.

Alas, something goes wrong here, but I *can* bind any arbitrary
attribute, and seems like the instance of OptimizedRectangle does have
its own __dict__ where those attributes are kept. I'm using the latest
stable version of Python (2.3.4, I believe). Here is a session from
IDLE:

Here I'm defining the class with __slots__:
__slots__ = 'width', 'heigth'


and create its instance, 'ropt':
Note that __dict__ is still there:{}

so I can define some arbitrary variable, say, newarea:

which goes into the dictionary
{'newarea': 15}

whereas width and heigth are still kept in __slots__:('width', 'heigth')

My impression is that once __slots__ are defined, __dict__ is
effectively disabled - but in this example it's alive and well. Am I
missing anything here?

TIA.
 
L

Larry Bates

I'll bet this is because you are subclassing Rectangle
class that is an old-style class. Maybe one of the
other "experts" on this can confirm.

Larry Bates
Syscon, Inc.
 
I

Ixokai

The problem is that the __slots__ mechanism is a function of "new style
classes", which really should be renamed =) A new-style-class is any class
that inherits from "object" or one of its descendants.

You're inheriting from Rectangle. What is its definition? I bet its
something like:

class Rectangle:
def __init__(self): pass

Since OptimizedRectangle inherits from an old-style class, its an old-style
class... and __slots__ has no function.

So, just...

class Rectangle(object): pass

And OptimizedRectangle should work like you expect.

--Stephen
 
P

Porky Pig Jr

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 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')

__slots__ = 'width', 'heigth'


TIA.
 
I

Ixokai

*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 said:
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.2def __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')

__slots__ = 'width', 'heigth'


TIA.
 
A

Anand Pillai

Since types and classes are unified in the new object model,
the following line at the top of the source file will also
do the trick, even if you dont directly subclass from object.

__metaclass__ = object

class Rectangle... etc

-Anand
 
M

Michele Simionato

Since types and classes are unified in the new object model,
the following line at the top of the source file will also
do the trick, even if you dont directly subclass from object.

__metaclass__ = object

You mean __metaclass__ = type, isn't it?


Michele Simionato
 
P

Porky Pig Jr

Ixokai said:
*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


Well, the fact is that the whole example is from 'Python in a
nutshell', by Alex Martelli, 2003 edition, should be pretty much up to
date, unless of course something got changed between the time this
book was written (probably some earlier version of 2.3 was already in
place) and the current release (2.3.3+), or the whole example is
wrong.

I'll try to contact the author directly.

Thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top