468,249 Members | 1,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

__slots__ in derived class

Hello,

consider this code
class A(object): .... def __init__(self):
.... self.a = 1
.... self.b = 2
.... class B(A): .... __slots__ = ["x","y"]
.... b=B()
b.a 1 b.b 2 b.x = 100
b.y = 100
b.z = 100
no exception here
does __slots__ nothing when used in derived classes?



class Z(object): .... __slots__ = ["x","y"]
.... z=Z()
z.x = 100
z.y = 100
z.z = 100 Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Z' object has no attribute 'z'


here it works like expected

Regards, Daniel

Mar 15 '06 #1
3 1356

Schüle Daniel wrote:
Hello,

consider this code
>>> class A(object): ... def __init__(self):
... self.a = 1
... self.b = 2
... >>> class B(A): ... __slots__ = ["x","y"]
... >>> b=B()
>>> b.a 1 >>> b.b 2 >>> b.x = 100
>>> b.y = 100
>>> b.z = 100
no exception here
does __slots__ nothing when used in derived classes?

>>>
>>>
>>> class Z(object): ... __slots__ = ["x","y"]
... >>> z=Z()
>>> z.x = 100
>>> z.y = 100
>>> z.z = 100 Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Z' object has no attribute 'z' >>>


here it works like expected

Regards, Daniel


I would expect that A has to define its own __slots__ too.

The following code should work as expected and makes also sense with
the memory optimization considerations that motivated introduction of
the __slots__ variable.

class A(object):
__slots__ = ["a","b"]
def __init__(self):
self.a = 1
self.b = 2

class B(A):
__slots__ = ["x","y"]

Kay

Mar 15 '06 #2
Schüle Daniel wrote:
consider this code
class A(object): ... def __init__(self):
... self.a = 1
... self.b = 2
... class B(A): ... __slots__ = ["x","y"]
... b=B()
b.a 1 b.b 2 b.x = 100
b.y = 100
b.z = 100


no exception here
does __slots__ nothing when used in derived classes?


__slots__ is intended as a way to reduce memory consumption. It was never
intended as a protection mechanism.

The slots which are available in a class only add to the attributes
available in the base class. You can hide base class slots by defining a
slot of the same name, but you cannot remove them.

Your base class has a __dict__ attribute and therefore all instances of the
base class or any derived classes also have a __dict__ attribute.
Mar 15 '06 #3
In article <dv**********@news2.rz.uni-karlsruhe.de>,
=?ISO-8859-1?Q?Sch=FCle_Daniel?= <uv**@rz.uni-karlsruhe.de> wrote:

does __slots__ nothing when used in derived classes?


Short answer: don't use __slots__ until you're comfortable writing
metaclasses and decorators. __slots__ are a performance hack strictly
for advanced users, and if you think you need them, you probably don't.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"19. A language that doesn't affect the way you think about programming,
is not worth knowing." --Alan Perlis
Mar 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jean Brouwers | last post: by
3 posts views Thread by Nick Jacobson | last post: by
7 posts views Thread by Porky Pig Jr | last post: by
1 post views Thread by Nitin Shukla | last post: by
2 posts views Thread by Ewald R. de Wit | last post: by
15 posts views Thread by David Isaac | last post: by
1 post views Thread by pascal.parent | last post: by
3 posts views Thread by John Machin | last post: by
27 posts views Thread by Licheng Fang | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.