In my code below, I am defining two functions.
>>> def main():
number = int(input('Enter a number: '))
cubed_number = cube(number)
print("The number cubed is: ", cubed_number)
>>> def cube(number):
return number * number * number
Enter a number: 5
The number cubed is: 125
>>> class Main:
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
File "<pyshell#27>", line 2, in Main
NameError: name 'cubed_number' is not defined
To understand what is happening, one must understand the distinction Python makes between defining a function, and executing a function.
When Python encounters a function definition, it compiles the function into a code object.
A code object is an internal structure Python uses to hold the bytecode associated with a specific executable block of code. It also holds other information Python needs to execute the bytecode such as constants and local variable names. The documentation gives a much more more extensive overview of what code objects are.
The code object is then used to construct a function object. The function object's code object is then used to execute the function when it is later called. Python would not execute the function, it would only compile the function into an object that could be used later for execution. The only time Python executes a function, is when the function is called.
Here is the relevant part from the documentation which mentions this:
A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called.
The function definition does not execute the function body; this gets executed only when the function is called.
Because of this distinction, Python cannot verify a name is actually defined until the function is called. Thus, you are allowed to use currently non-existent names in the function body. As long as the name is defined when the function is called, Python will not raise an error.
Here is an example. We define a function
func that adds two variables together;
>>> def func(): ... return a + b
As you can see Python raised no error. This is because it simply compiled
func. It did not attempt to execute the function, so it does not see that
b are not defined.
We can disassemble
func's code object, and see what the bytecode looks like using the
dis module. This will tell us more about what Python is doing:
>>> from dis import dis >>> dis(func) 2 0 LOAD_GLOBAL 0 (a) 2 LOAD_GLOBAL 1 (b) 4 BINARY_ADD 6 RETURN_VALUE
Python encoded two
LOAD_GLOBAL instructions in the byte-code. The arguments to the instructions are the variable names
This shows that Python did see that we were attempting to reference two variables when compiling our function and created bytecode instructions to do so. But it does not attempt to actually execute the instructions, until the function is called.
Let's see what happens when we attempt to execute the bytecode for
func by calling it:
>>> func() Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> func() File "<pyshell#14>", line 2, in func return a + b NameError: name 'a' is not defined
As you can see, Python raised a
NameError. This is because it attempted to execute the two
LOAD_GLOBAL instructions, but discovered that the names where undefined in the global scope.
Now let's see what happens if we defined two variables
b before calling
>>> a = 1 >>> b = 2 >>> >>> func() 3
The reason the above worked, is because when Python was executing
func's byte-code, it was able to find the global variables
b, and used those to execute the function.
The same applies to the example is the question. When
main was compiled, Python "saw" we were trying to call a variable named
cube and generated an instruction to get the value of
cube. But it did not attempt to find a callable object named
cube until the instructions were executed. And by the time
main's byte-code was executed(eg.
main was called), a function named
cube was defined, so Python did not raise an error.
If we try to call main before cube is defined however, we'll get a name error for the same reasons in the above example:
>>> def main(): ... number = int(input('Enter a number: ')) ... cubed_number = cube(number) ... print("The number cubed is: ", cubed_number) ... >>> main() Enter a number: 23 Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> main() File "<pyshell#22>", line 3, in main cubed_number = cube(number) NameError: name 'cube' is not defined
Python handles class definitions a bit differently from function definitions.
When Python encounters a class definition, it creates a code object for the class as with the function. However, Python also allows classes to have namespaces that are executed during the class definition. Python does not wait to execute the classes namespace because any variables defined should belong to the class. Thus, any names used inside of a classes name-space must be defined for usage during class-definition time.
The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved.
However, this does not apply to methods. Python treats undefined names in methods as with functions, and allows you to use them when defining the method:
>>> class Class: ... def method(self): ... return var ... >>> var = 10 >>> cls = Class() >>> cls.method() 10