Python bytecode function call passing self

Viewed 474

I'm trying to understand how bytecode works.

a.func() is a function call. The corresponding bytecode is roughly LOAD_GLOBAL a, LOAD_ATTR attr and then CALL_FUNCTION with 0 arguments.

This is totally fine if a is a module. But if a is an object, it has to pass the object instance itself. Since Python could NOT know whether a is a module or an object at compile time, naturally the bytecode is same regardless of the type of a. But how does the runtime system handle self as the first argument to func if a is an object? Is there some special handling below bytecode level that says "if it is called on an object prepend the object as the first argument"?

3 Answers
Related