Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason to prefer list or tuple for __slots__?

Tags:

python

You can define __slots__ in new-style python classes using either list or tuple (or perhaps any iterable?). The type persists after instances are created.

Given that tuples are always a little more efficient than lists and are immutable, is there any reason why you would not want to use a tuple for __slots__?

>>> class foo(object):
...   __slots__ = ('a',)
... 
>>> class foo2(object):
...   __slots__ = ['a']
... 
>>> foo().__slots__
('a',)
>>> foo2().__slots__
['a']
like image 671
ʞɔıu Avatar asked Jan 03 '14 23:01

ʞɔıu


People also ask

Should I use a tuple for__slots__?

You can define __slots__ in new-style python classes using either list or tuple (or perhaps any iterable?). The type persists after instances are created. Given that tuples are always a little more efficient than lists and are immutable, is there any reason why you would not want to use a tuple for __slots__? Show activity on this post.

What is the difference between tuples and lists in Java?

The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified. Tuples are more memory efficient than the lists.

What are the additional functionalities associated with a list over tuple?

We can clearly see that, there are so many additional functionalities associated with a list over a tuple.We can do insert and pop operation, removing and sorting elements in the list with inbuilt functions which is not available in Tuple.

What is tuples in Python?

Tuples are commonly used as the equivalent of a dictionary without keys to store data. For Example, Above example contains tuples inside list which has a list of movies.


1 Answers

First, tuples aren't any more efficient than lists; they both support the exact same fast iteration mechanism from C API code, and use the same code for both indexing and iterating from Python.

More importantly, the __slots__ mechanism doesn't actually use the __slots__ member except during construction. This may not be that clearly explained by the documentation, but if you read all of the bullet points carefully enough the information is there.

And really, it has to be true. Otherwise, this wouldn't work:

class Foo(object):
    __slots__ = (x for x in ['a', 'b', 'c'] if x != 'b')

… and, worse, this would:

slots = ['a', 'b', 'c']
class Foo(object):
    __slots__ = slots
foo = Foo()
slots.append('d')
foo.d = 4

For further proof:

>>> a = ['a', 'b']
>>> class Foo(object):
...     __slots__ = a
>>> del Foo.__slots__
>>> foo = Foo()
>>> foo.d = 3
AttributeError: 'Foo' object has no attribute 'd'
>>> foo.__dict__
AttributeError: 'Foo' object has no attribute '__dict__'
>>> foo.__slots__
AttributeError: 'Foo' object has no attribute '__slots__'

So, that __slots__ member in Foo is really only there for documentation and introspection purposes. Which means there is no performance issue, or behavior issue, just a stylistic one.

like image 94
abarnert Avatar answered Sep 22 '22 02:09

abarnert