$ 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
Bug report
Bug description:
Compare these two
timeitinvocations:Observe that in the case where
Test.xis a slot,hasattrtakes 241 ns to returnFalsewhile it only takes 33.5 ns when there is noTest.xslot.Peter Bierma advised me to file an issue when I asked about this in Discourse.
CPython versions tested on:
3.14, 3.13, 3.12
Operating systems tested on:
Linux
Linked PRs
hasattr#157866