friends , If you need to reprint, please indicate the source :https://blog.csdn.net/jiangjunshow
Statement : During the teaching of artificial intelligence technology , Many students mentioned to me python Related issues , So in order to let the students master more extended knowledge and better understand AI technology , I put the assistant in charge of sharing this python Series of tutorials , I hope that helps ! Because of this python The tutorial wasn't written by me , So it's not as good as mine AI Technology teaching is humorous , It's boring to learn ; But its knowledge is in place , It's also worth reading ! Want to learn AI Technical students can click to jump to my Teaching website .PS: If you don't understand this article, please read the previous article first , Step by step, learn a little everyday, and you won't find it difficult !
We can run dir Call it to have a look python Which variable names are predefined :
>>>import builtins
>>>dir(builtins)
['ArithmeticError','AssertionError','AttributeError','BaseException',
'BufferError','BytesWarning','DeprecationWarning','EOFError','Ellipsis',
...many more names omitted...
'print','property','quit','range','repr','reversed','round','set',
'setattr','slice','sorted','staticmethod','str','sum','super','tuple',
'type','vars','zip']
The variable names in this list make up Python Built in scope in . In a nutshell , The first half is the built-in exception , The latter half is built-in function . because LEGB The laws of Python Finally, the module will be searched automatically , All variable names in this list will be automatically obtained . in other words , You can use these variable names without importing any modules . therefore , There are two ways to reference a built-in function : adopt LEBG The laws of , Or manually import __builtin__ modular .
>>>zip # The normal way
<class 'zip'>
>>>import builtins # The hard way
>>>builtins.zip
<class 'zip'>
The second implementation is sometimes useful in complex tasks . Careful readers may have noticed that due to LEGB Find the process , Will make it take effect where the first variable name is found . in other words , Variable names in the local scope may override variables with the same variable name in the global scope and the built-in scope , The global variable name may override the built-in variable name . for instance , A function creates a function called open And assigned a value to it :
def hider():
open = 'spam' # Local variable,hides built-in
...
open('data.txt') # This won't open a file now in this scope!
In this case , The built-in scope will be named open The built-in functions of . This is often a Bug, And the headache is , because Python This problem is not handled as a warning message ( Because in advanced programming, you may want to replace the built-in variable name by predefining the variable name in the code ).
Function can also simply hide global variables with the same name by using local variable names .
X = 88 # Global X
def func():
X = 99 # Local X: hides global
func()
print(X) # Prints 88: unchanged
here , The assignment statement inside the function creates a local variable X, It is associated with the global variables of the module file outside the function X Is a completely different variable . That's why , If in def No increase within global( or nonlocal) In a statement , There is no way to change variables outside a function inside a function .