473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

__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 1555

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jean Brouwers | last post by:
Classes using __slots__ seem to be quite a bit smaller and faster to instantiate than regular Python classes using __dict__. Below are the results for the __slots__ and __dict__ version of a...
3
by: Nick Jacobson | last post by:
The __slots__ attribute of new-style classes can reduce memory usage when there are millions of instantiations of a class. So would a __slots__ attribute for functions/methods have a similar...
7
by: Porky Pig Jr | last post by:
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...
1
by: Nitin Shukla | last post by:
Hello, Can anyone tell why am I getting this error and how to work around this problem. >>> class Klass(object): .... __slots__ = ( 'x' ) .... >>> class Klass1(object): .... __slots__ =...
2
by: Ewald R. de Wit | last post by:
I'm running into a something unexpected for a new-style class that has both a class attribute and __slots__ defined. If the name of the class attribute also exists in __slots__, Python throws an...
15
by: David Isaac | last post by:
1. "Without a __dict__ variable, instances cannot be assigned new variables not listed in the __slots__ definition." So this seemed an interesting restriction to impose in some instances, but...
1
by: pascal.parent | last post by:
Hi, I try to define a (new-style) class who: - have a __slots__ defined to be strict attributes, - return None if the attribute is 'ok' but not set, or raise a 'normal' error if the attribute...
3
by: John Machin | last post by:
I have stumbled across some class definitions which include all/most method names in a __slots__ "declaration". A cut-down and disguised example appears at the end of this posting. Never mind...
27
by: Licheng Fang | last post by:
Python is supposed to be readable, but after programming in Python for a while I find my Python programs can be more obfuscated than their C/C ++ counterparts sometimes. Part of the reason is that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.