__slot__: what is it good for?

  • Thread starter Alexander Eisenhuth
  • Start date
A

Alexander Eisenhuth

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
 
P

Peter Otten

Alexander said:
- 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
 
S

Stephen Hansen

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/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMH4QmAAoJEKcbwptVWx/lMbkH/R7yeT3uAWMcQvjgxjGY5PTP
n7L0f3q9CdRKItIFjcfGJ9uUTQBcrZ8VKSg1pME21A3rDT+ElkA8iR01qEQ6PsOK
1Z4wL3hbsEQ5SSYBNZYPHq1rPmWi/lJ0CqNPn48IY7yRgsztQnv50PAJ2Q04ietj
yNeAhIQfbrF15o7ZQ90DHu+Zm7+ZNtQa+MnBwLYwoLi20gJQk3dt5hAFXyLqnPG3
E/pa217QEuXLBb/wRDteKZcAli2SUGYsnAdU81cuPwcQRPl+xY7p/yNX1EWs620x
JVesXnSoLXq2zgD+7R0HxHcagZJYVhAtywO8zOrlfZxNLDjYNbilza9DFfT5tqY=
=pPJW
-----END PGP SIGNATURE-----
 
T

Terry Reedy

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.

Or add a __setattr__ method, but the above is easier.
 
S

Stephen Hansen

Or add a __setattr__ method, but the above is easier.

I'd sort of argue that adding a __setattr__ for that purpose is
un-Pythonic (as opposed to using __setattr__ for some functional purpose
and that restriction being a side-effect). Its possible to do it, sure;
but its also possible to use __slots__ to do so as well. But its bad
form in both cases.

But, that argument is largely Nit meeting Pick and having a fight about
which kettle is blacker. :)

--

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



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMH7WUAAoJEKcbwptVWx/ldKoH/AsDuR7je1zUi7KCpQb/dWAW
sb8vHU6cqu95hVE848wGnl2ajzjEWyZPVu/J1bbur37Zrd/DHOYNAeFygf8oW6/4
RrFBBoQUYjaEneogwM7oUSdK8iNaWawtzsFxPEVNKbnG0qHHodykv8WvSIZLkrly
nIZZHCn0tIWe1drsuRfVdg+f6l66Ak57yuDpspJRiTkVVwFcphKzmsivo7rzkBn1
5W3y0O1S5zlJ9wk2p8fUhd5aMC03kAcWICLOpCHx0OOXt1APD48f3+98mLP9XYBD
QGOx0+T7ynz7EbiY1xirPgVKITRQBXVnpk9cxXdgDNlbmF6skUbmBP9+whVqAdI=
=+VzQ
-----END PGP SIGNATURE-----
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top