From: Alexander Eisenhuth on
Hello out there,

- what is the reason, that __slots__ are introduced in python?

- I want to use slots to define a class where no attributes are added at
runtime. Is that a good idea to use slots for that?


Regards
Alexander
From: James Mills on
On Tue, Jun 22, 2010 at 12:27 AM, Alexander Eisenhuth
<newsuser(a)stacom-software.de> wrote:
> Hello out there,
>
> - what is the reason, that __slots__ are introduced in python?
>
> - I want to use slots to define a class where no attributes are added at
> runtime. Is that a good idea to use slots for that?

Here is the relevant documentation:'

http://docs.python.org/reference/datamodel.html#slots

--
--
-- "Problems are solved by method"
From: Peter Otten on
Alexander Eisenhuth wrote:

> - what is the reason, that __slots__ are introduced in python?

When you have "many" instances of a class with a fixed set of attributes
__slots__ can save you some memory because it avoids the overhead for the
instance __dict__. Note that "many" means millions rather than thousands.
The best approach is to measure the memory footprint of your app with and
without __slots__ in the class you see as a good candidate and only use it
when you find a significant difference.

> - I want to use slots to define a class where no attributes are added at
> runtime. Is that a good idea to use slots for that?

No.

Peter

From: Stephen Hansen on
On 6/21/10 7:27 AM, Alexander Eisenhuth wrote:
> Hello out there,
>
> - what is the reason, that __slots__ are introduced in python?
>
> - I want to use slots to define a class where no attributes are added at
> runtime. Is that a good idea to use slots for that?

In short, its best to use __slots__ in situations where you are creating
a large number of objects with a set few number of attributes. It lets
you pre-define those variables and the descriptors to access them,
without having to "waste" a dictionary. Unless you're making many
objects, the "waste" of the instance dictionary is largely irrelevant.

Its an optimization: its not an access-control mechanism, or a mechanism
designed to "declare" the attributes of a class before-hand or prevent
attribute creation at runtime.

You could use it that way: but its bad form :)

If you don't want a class to have attributes added at runtime, the
Pythonic way to achieve that is to... simply add attributes at runtime.

Python doesn't really believe in mandates. It believes in good behavior
of adults, awhile acknowledging sometimes an adult might have a
perfectly good reason to do something sneaky (such as add an attribute
to an instance at runtime: its actually *extremely* rare for someone to
do that, so why try to force it away?) down the road that you might not
think of at the time.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/

From: Stephen Hansen on
On 6/21/10 8:08 AM, Stephen Hansen wrote:
> If you don't want a class to have attributes added at runtime, the
> Pythonic way to achieve that is to... simply add attributes at runtime.

Errr.

The Pythonic way to achieve that is to... simply NOT add attributes at
runtime.

I.e., choose to follow the rule you've decided on.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/