Namespace function

There are three namespaces

Namespace has three 
built-in namespace - python interpreter
is python interpreter to store a name you can start to use the built-in namespace
built-in names are loaded into memory at boot time interpreter in
the global namespace - we write code but not the function of the code
is to be executed during the program from top to bottom in order to load into memory
placed all variables and function names that we set up
a local namespace - function
is defined inside a function name
when calling functions when it will produce the namespace with the end of the function performed by this namespace disappear again

Local: You can use a global, built-in namespace names 
in global: You can use the built-in namespace names, but you can not use topical in
the built-in: You can not use the names of local and global
def func():
    a = 1

func()
print(a)

def max(l):
    print('in max func')

print(max([1,2,3]))

 

Under normal circumstances, the direct use of the built-in name 
when we define the name and namespace of the same name built in the global, will use a global name
when I myself sometimes I do not find my superiors asked for
if he did it find the one you want to find on a higher level if there is no built-in namespace are not just being given
more functions should have a more independent local namespace, do not share with each other
def input():
    print('in input now')
def func():
    # input = 1
    print(input)
func()
def fun1():
    a = 1

def fun2():pass

 

func -> memory address of the function 
function name () function is called
memory address of the function () function is called


the scope of two kinds of
global scope - the whole picture - and built the global namespace names are part of the global role domain --globals ()
local scope - role in local - function (local namespace name belonging to local scope) --locals ()
a = 1
def func():
    global a
    a = 2

func()
print(a)
For immutable data types, but view the global scope of local variables 
but can not be modified directly
if you want to modify, you need to add global statement at the beginning of the program
If you declare a global variable in a local (function), then the variable valid for local operations will all global variables
a = 1 
b = 2
 def func (): 
    x = ' aaa ' 
    y = ' bbb ' 
    print (local ())
     print (Global ()) 

func () 
print (Global ())
 print (local ()) # 本地的
globals always print the global name 
locals where the locals what position according to the output
a = 1
def func(a):
    a = 2
    return a

a = func(a)
print(a)

 



Guess you like

Origin www.cnblogs.com/rxiaoxi/p/11923316.html