Python function variable Lookup order: Priority: Local---> Global---> If still not found, a nameerror error will be raised.
#!/usr/bin/env python#coding:utf-8# @Author: andy# Date:2017/6/14money = 1000def tell_info (name):p rint ("%s has%d"% (NA Me, Money)) def fun (): Money = 10tell_info (' Egon ') Fun () # Egon has 1000# tell_info function executed inside the fun function, but still needs to go back to the definition to find the relevant variable # Priority from the local find, the local not find the global, because the function meaning when the money=1000, and the execution position of the money=10, regardless. Money = 1000def F1 (): Money = 10def tell_inform (name):p rint ("%s With%d "% (name, money)) Tell_inform (' Egon ') F1 () # Egon has the 10# tell_inform function defined inside the F1, and there is a definition of the local variable, so the lookup variable value is found locally that stops # Will not be looked at globally, so money ends up with a 10 "" function scope is irrelevant to where the function is executed, only with respect to the definition function. Whichever call goes back to the definition phase to find the corresponding scope relationship. The lookup order is: local---> Global---> Nameerror ""
Python function variable Lookup order