06Jul

Python: What the Hell is a Slot?

Posted by Elf Sternberg as programming, python

So, while working my way through my current project, I came across the notion of a ‘slot’ as discussed here. And I wondered, what the Hell is a slot?

A slot is nothing more than a memory management nicety: when you define __slots__ on a class, you’re telling the Python interpreter that the list of attributes described within are the only attributes this class will ever need, and a dynamic dictionary is not needed to manage the references to other objects within the class. This can save enormous amounts of space if you have thousands or millions of objects in memory.

For example, if you define:

class Foo:
    __slots__ = ['x']
    def __init__(self, n):
        self.x = n

y = Foo(1)
print y.x  # prints "1"
y.x = 2
print y.x  # prints "2"
y.z = 4    # Throws exception.
print y.z

Without the __slots__ definition, that last assignment would have worked.  Without any other magic (say, overrides of getattr or setattr), you can always assign attributes to an object.  But with a __slots__ definition, you can’t: python hasn’t allocated a dictionary for the object, so you’re stuck with what you’ve got and nothing more.

Slots should only be used as a memory optimization tool; using it to constrain attribute management is silly, and breaks important features like static serialization.

9 Responses to Python: What the Hell is a Slot?

Bram

July 6th, 2009 at 3:22 pm

David W

July 6th, 2009 at 3:58 pm

It’s worth note that there are various gotchas regarding slots, particularly since they’re implemented as descriptors. One of my preferred tricks, defining a class variable to act as a default for an instance variable, cannot work, since the slot’s descriptor needs to be in the class dictionary.

It’s also worth note that slots layer up – a little confusingly at first, but quite understandably. You can define a class X(slots=a) and subclass Y(slots=b) and things will work as expected. I have no idea how memory is allocated for that underneath. Perhaps Python’s type object is smart enough to merge them into a single object.

The other last thing worth note is that slots can have a very positive impact on performance, not just on memory usage.

Great post,

David (dwdwdw@Reddit!)

Simon

July 7th, 2009 at 2:31 am

This only works on new style classes. Your example should be updated to read:

class Foo(object):
__slots__ = ['x']
def __init__(self, n):
self.x = n

y = Foo(1)
print y.x # prints “1″
y.x = 2
print y.x # prints “2″
y.z = 4 # Throws exception.
print y.z

Cheers though! Very helpful!

Chewie

July 7th, 2009 at 4:07 am

Warning: unless you’re using Python 3000, you must use new-style classes for __slots__ to work, i.e., you should write class Foo(object):.

StCredZero

July 7th, 2009 at 8:03 am

Smalltalk just has the slots. Slots are easier to optimize and make fast with a JIT VM. If you need a class to have the functionality of a Hashtable, you just put a Dictionary into an instance variable. (Then you have to write some plumbing code, which is not so convenient.)

This ability to add slots allows one to put in optimizations if needed, but keeps the default case simple and flexible. I like that.

Basu

July 7th, 2009 at 11:39 am

One of the philosophical (and somewht practical) dilemmas that i find myself thinking about as a programming languages buff is how much work should the language do and how much should the programmer do. I personally try to stay away from manual management of any sort, but sometimes performance requires it. It’s unfortunate, but soemthing that needs to be done sometimes. I think language features like slots are a good way to bypass the languages dynamic, automated features but should be used only in the final optimization stage of a program’s development cycle.

LE

July 29th, 2009 at 7:54 am

@Manual management guy above:

No, you can implement the slots in pure Python if you’d like. You’ll be wasting a perfectly good dictionary on each object, but that’s still less processing overhead.

What slots do is they essentially create descriptors for each attribute named. For example: http://pb.lericson.se/p/SkRPZb/

Descriptors are used for implementing static methods, class methods, unbound/bound methods, and properties. They’re very essential to Python’s OOP. Nothing weird.

How to Create “Immutable” Classes in Python | Hello Linux

January 17th, 2014 at 8:20 pm

[...] What the H*ll is a slot? [...]

Malhar Vora

February 15th, 2014 at 3:07 am

Really nice and informative article. Most of my python scripts do not use assign attributes dynamically to class so I guess I can take advantage of __slots__.

Thank you.

Comment Form

Recent Comments