Compare these two timeit invocations:
$ python3.14 -m timeit -s 'class Test: pass' -s 't = Test()' 'hasattr(t,"x")' 10000000 loops, best of 5: 33.5 nsec per loop $ python3.14 -m timeit -s 'class Test: __slots__ = ("x",)' -s 't = Test()' 'hasattr(t,"x")' 1000000 loops, best of 5: 241 nsec per loop
Observe that in the case where Test.x is a slot, hasattr takes 241 ns to return False while it only takes 33.5 ns when there is no Test.x slot.
Peter Bierma advised me to file an issue when I asked about this in Discourse.
3.14, 3.13, 3.12
Linux
gh-157840: Special-case member descriptors when using `hasattr` (GH-1…
Activity
gh-157840: Special-case member descriptors when using `hasattr` (GH-1…