question: usage of __slots__

T

T. Kaufmann

Hi there,

Can somebody give me some good examples of the usage of __slots__?

Thanks for help.

o-o

Thomas
 
A

Alex Martelli

T. Kaufmann said:
Can somebody give me some good examples of the usage of __slots__?

There aren't all that many, but here's one:

class point(object):
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y

thanks to the __slots__, each instance of point takes up little memory (in a
32-bit machine, and assuming x and y are typically floats, about 40 bytes,
I believe), saving the memory overhead of an instance dictionary (which
would more than double its size). If you have millions of such instances in
memory at once, that matters! There's a price to pay in terms of
flexibility (can't simply annotate a given point by adding an attribute to
it on the fly any more), but it may be considered worth paying when saving
lots and lots of memory calls for it.


Alex
 
A

Alexander Schmolck

T. Kaufmann said:
Hi there,

Can somebody give me some good examples of the usage of __slots__?

__slots__ are a premature optimization and best avoided.

'as
 
R

Rainer Deyke

Alexander said:
__slots__ are a premature optimization and best avoided.

__slots__ is also an error-catching mechanism.

class A(object):
__slots__ = ['spam']
pass

A().spaam = 0 # Caught at assign time.
 
J

Jeremy Fincher

Alexander Schmolck said:
__slots__ are a premature optimization and best avoided.

It's nice to see that there's *someone* out there with a handle on
every single possible use for Python.

Jeremy
 
M

Michael Hudson

Rainer Deyke said:
__slots__ is also an error-catching mechanism.

Yes, but it's intent is as a memory saving optimization. Really. In:

http://mail.python.org/pipermail/python-dev/2003-August/037542.html

Guido says:

__slots__ is a terrible hack with nasty, hard-to-fathom side
effects that should only be used by programmers at grandmaster and
wizard levels. Unfortunately it has gained an enormous undeserved
popularity amongst the novices and apprentices, who should know
better than to use this magic incantation casually.

Cheers,
mwh
 
D

Dave Benjamin

Are you arguing with Guido?

If he is, he's being implicit and not explicit about it, which is definitely
bad style. There should be one and only one obvious way to argue with Guido.

<ducking>
 
J

Jeremy Fincher

Are you arguing with Guido?

Are you claiming that Guido would, without exception, call __slots__ a
"premature optimization"?

Seems it wouldn't have ever made it into the language if that was the
case.

Jeremy
 
A

Aahz

Are you claiming that Guido would, without exception, call __slots__ a
"premature optimization"?

No, but I don't think he would disagree with that statement in casual
conversation.
 
J

Jeremy Fincher

No, but I don't think he would disagree with that statement in casual
conversation.

I think he would quantify such a statement, which is why I challenged
the universality of the original statement, not its content in
general.

Jeremy
 
M

Martin Maney

No, but I don't think he would disagree with that statement in casual
conversation.

I dunno, greeny. That bit about being surprised (and perhaps appalled)
at its popularity with novices suggests he might prefer to call it an
optimization for the post-mature programmer...

--
Some kinds of waste really are disgusting. SUVs, for example,
would arguably be gross even if they ran on a fuel which would
never run out and generated no pollution. SUVs are gross because
they're the solution to a gross problem. (How to make minivans
look more masculine.) -- Paul Graham
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top