A function is not its source text. When Python defines a function, it keeps a code object: the bytecode, and the facts that the bytecode needs, such as the names of the variables and the constants. The virtual machine runs that bytecode with a stack. You can open a code object, list its instructions with the dis module, and see for yourself why some code is faster than other code, and why a variable is local or not.
More lessons in Complete Python Programming