On Mon, Aug 20, 2018 at 7:05 PM, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote: > Marko Rauhamaa wrote: >> >> Chris Angelico <ros...@gmail.com>: >> >>> 3) Every invocation of method() has to execute the class body, which >>> takes time. >> >> >> That's what happens with every method invocation in Python regardless. > > > No, it doesn't! Invoking a method involves creating a bound method > object, which is very small and lightweight. Executing a class > statement creates a class object, which is enormous by comparison, > and quite expensive to initialise.
Additionally, "creating a bound method object" is an action which can be optimized, or even completely optimized out. There have been proposals to peephole-optimize "x.method(arg)" to a single opcode, while still maintaining the proper semantics for "f = x.method; f(arg)", so the creation of the bound method would be skipped where it's unnecessary. (I think PyPy was looking at something like this? Not sure.) Even if the bound method is created, proper use of free lists can make it pretty efficient. Creating a new class object, on the other hand, MUST be performed sequentially, executing each of the statements inside it. ChrisA -- https://mail.python.org/mailman/listinfo/python-list