The title was carefully worded as I have no idea how or why what is happening is happening, only that it has been like this since a least 3.6.0. That version in particular, by the way, is able to execute a call to a python function with 1 argument 25% faster than 3.8.0 but that may be due at least in part by whatever it is that makes it much faster to a call a unary function wrapped by functools.partial by utilizing the subcript operator on an instance of a partial subtype whose __getitem__ has been set to the data descriptor partial.func... Eg:
class Party(partial): __getitem__ = partial.func
fast = Party(hash)
slow = partial(hash)
# the expression `fast[""]` runs approximately 28% faster than
# the expression `slow("")`, and partial.func as __getitem__ is
# confusingly 139% faster than partial.__call__...
That rather large digression aside, here's a demonstration of two functions identical in every way except the CO_NESTED bit and perhaps the names:
if 1:
def Slow():
global Slow
class Slow:
global slow
def slow(self): return self
return Slow
if Slow():
class Fast:
global fast
def fast(self): return self
import dis
dis.show_code(slow)
print()
dis.show_code(fast)
|