Skip to content

Conversation

@AlexWaygood
Copy link
Member

This appears to have existed in the docstring for 20 years without anybody noticing. It's a very minor issue, but the example __slots__ definition in this docstring actually creates 6 distinct slot descriptors on the class: 'n', 'u', 'm', 'b', 'e' and 'r'. I think this is almost certainly not what the example means to show.

We do actually point to this docstring from the docs on docs.python.org: https://docs.python.org/3/library/threading.html#threading.local

This appears to have existed in the docstring for 20 years without anybody noticing. It's a very minor issue, but the example `__slots__` definition in this docstring actually creates 6 distinct slot descriptors on the class: 'n', 'u', 'm', 'b', 'e' and 'r'. I think this is almost certainly not what the example means to show.
@kumaraditya303
Copy link
Contributor

I think this works fine, python allows a single string as special case.

@AlexWaygood
Copy link
Member Author

Ugh, what? Wow, that's counterintuitive:

>>> class Foo:
...     __slots__ = 'bar'
...     
>>> f = Foo()
>>> f.bar = 'baz'
>>> f.spam = 'what'
Traceback (most recent call last):
  File "<python-input-10>", line 1, in <module>
    f.spam = 'what'
    ^^^^^^
AttributeError: 'Foo' object has no attribute 'spam' and no __dict__ for setting new attributes
>>> list(Foo.__slots__)
['b', 'a', 'r']

Thank you!! Serves me right for not double-checking before filing a PR...

@AlexWaygood AlexWaygood deleted the threading-local-docstring branch December 14, 2024 14:24
@Eclips4
Copy link
Member

Eclips4 commented Dec 14, 2024

>>> list(Foo.__slots__)
['b', 'a', 'r']

Here, __slots__ is just a string, which is actually iterable, and that's why it happens :)

>>> list("meow")
['m', 'e', 'o', 'w']
>>>

Everyone makes mistakes. This happens to all of us 🙂

@AlexWaygood
Copy link
Member Author

Here, __slots__ is just a string, which is actually iterable, and that's why it happens :)

Right! That's why I thought that Python would iterate over the string and create 6 unique slot descriptors on the class, since that's what it does if you assign any other iterable to __slots__, such as a tuple, list, dictionary or even collections.deque

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants