function scope
When the Python function runs, it creates its own scope, which is the scope (or the function has its own namespace, or namespace). When executing a function, if a variable name is encountered in the body of the function, Python first looks for the variable within the function's namespace. If it is not found, it will jump out of the function in the global search (if it is nested functions will find a first-level function, go to the top, and then in the built in the search, can not find the error).
First, define a simple function
def test ():
A = 1
Print (1)
Test ()
What happens if I print a out on the outside?
Effect
Description of A's scope exists within the function
If it is possible to define a function outside of a
The value of a can be modified inside the function, but cannot be changed outside the value
In fact, the function doesn't change the value outside, it just re-creates a value like an external variable inside the function.
Print out the address to prove it.
A = 1
def test ():
A = 2
Print (a)
Print (ID (a))
Test ()
Print (a)
Print (ID (a))
The addresses are different.
If an external variable is used inside a function, it is the external variable that is used directly (no new variable is created)
Global variables are declared throughout the py file and can be accessed at the global scope
A local variable is declared in a function and can be called only in that function, and if an attempt is made to invoke it in an out-of-scope place, an error is made.
A function can call a global variable (that is, a global variable that is used directly)
If the local variable is the same as the global variable, then the global variable can be modified inside the function (only inside the function, not on the outside), in fact, the temporary variable that is recreated internally is just like the global variable, essentially without any effect on the global variable.
The function basis can be referenced
http://blog.51cto.com/linuxubuntu/2085265
The scope of a function is also called a local scope, and its range is the entire function, out of which the function does not work
Scope chain
Name = "Jack"
Def f1 ():
Name = "Mike"
def f2 ():
Name = "Tom"
Print (name)
F2 ()
Print (name)
F1 ()
Print (name)
#执行过程为: Call the F1 () function, define the name and F2 () function,
#执行f2 () function, print the name in F2 ()
Name in #然后打印f1 (), and finally print the name of the global variable
There are scope chains in Python,
Variables will be found inside out, first go to their own scope to find,
I did not go to the superiors to find, until the error can not be found
For example:
After removing the internal variables
Name = "Jack"
Def f1 ():
#name = "Mike"
def f2 ():
#name = "Tom"
Print (name)
F2 ()
Print (name)
F1 ()
Print (name)
Print Address
You can see that all the addresses of global variables
Similar
Def f1 ():
Name = "Jack"
def f2 ():
Print (name)
F2 ()
F1 ()
Called a nested function, which is one or more functions inside a function
A function can use an external variable but cannot modify an external variable, and an external variable cannot modify a variable inside the function.
If you want to do something outside of the data modification inside the function, you can return the function address internally and make the modification
Functions inside the function return the address directly after the data has been modified so that the in-memory data is not recycled for the purpose of externally invoking the modification.
That's normal.
Def f1 ():
b = 1
def F2 (b):
B + = 1
Print (b)
F2 (b)
F1 ()
F1 ()
F1 ()
F1 ()
F1 ()
After the code has been modified:
Def f1 ():
def F2 (b):
B + = 1
Return b
return F2
Print (F1 ())
Print (F1 () (1))
You can see that the call F1 () gets a function address
You can assign F1 () to a variable and then execute the
This is a simple closure.
The closure is when you call a function A, and the function a returns a function B to you. The returned function, B, is called a closure. The argument you pass when you call function A is a free variable.
When called F1, a closure--f2 is generated, and the closure holds the free variable--b, so it also means that after the life cycle of the function F1 is over, the B variable still exists because it is referenced by the closure and is not recycled.
Decorative Device
An adorner is essentially a Python function that adds functionality without changing the original function.
For example:
Winter in order to wear warm clothing (basic needs), in order to enhance the warmth of the effect can be wearing a hat windproof glasses gloves and other configuration (new requirements)
#给下面函数加一项输入的功能
Def f1 ():
Print (' F1 ')
def f2 ():
Print (' F2 ')
Def f3 ():
Print (' F3 ')
The first way:
Adding the same code to each function is too inefficient
The second way:
Wrapping a new function into a function is reduced in each function, the efficiency is increased, but violates the open closure principle
The core idea of the open closure principle is:
Software entities should be extensible and non-modifiable. In other words, the extension is open, and the modification is closed.
Therefore, the principle of open closure is mainly embodied in two aspects:
Opening up to extensions means that existing code can be extended to accommodate new situations when there are new requirements or changes.
Enclosing a modification means that once the class is designed, it can do its work independently, rather than making any modifications to the class.
The simple thing is,
Closed: Implemented function code block is not modified
Open: Extend Existing code
In Python everything is object, so in Python, the function itself is an object, in the global domain, the function object is referenced by the function name, so it is possible to refer to the function object with other variable names, which is equivalent to changing the name.
Like what:
def test ():
Print ("Test")
Calls can be called directly
Test ()
can also be fu=test
Fun ()
This invokes
def test ():
Print ("Test")
FU = Test
Fu ()
Since the function name can be assigned to a variable, it is possible to pass the assigned variable as a parameter to the function, and the answer is yes.
Change the code, please.
def msg (fun):
Fun ()
Inputword = input ("Please enter information:")
Print ("Output information:", Inputword)
Def f1 ():
Print (' F1 ')
MSG (F1)
function of normal output F1 function after call
This function can be added, but changed the way the call
The above call is F1 () changed here to MSG (F1),
Since assigning a function to a variable can be run with parentheses, can the address of the MSG be re-assigned to the variable? We can tell by the closure above that it's okay.
Change the code, please.
def msg (fun):
def func ():
Fun ()
Inputword = input ("Please enter information:")
Print ("Output information:", Inputword)
return func
Def f1 ():
Print (' F1 ')
F1 = MSG (F1)
F1 () #这样调用后就可以正常执行, in fact the Func function is executed here
This allows the functionality to be implemented without changing the calling method
This implements the adorner, but the syntax sugar @ in Python is implemented as follows:
def msg (fun): #在调用f1时执行过程为 msg equivalent to MSG (F1) fun equivalent to F1
def func ():
Fun ()
Inputword = input ("Please enter information:")
Print ("Output information:", Inputword)
return funcbr/> @msg
Print (' F1 ')
#f1 = MSG (F1)
F1 () #这样调用后就可以正常执行, in fact the Func function is executed here
Print out Address
You can see that the address of Func is the same as the address of F1
What if the decorated function has parameters? It's in the inside. Function Plus parameter
def msg (fun):
def func (ARG):
Fun (ARG)
Inputword = input ("Please enter information:")
Print ("Output information:", Inputword)
return funcbr/> @msg
Print (ARG)
F1 (' Hello World ')
If it is more than one parameter, *args and **kwargs will be used.
def Dec (fun):
def warpper (*args, *Kwargs):
Fun (args, **kwargs)
Print ("General decorator case")
return warpperbr/> @dec
Print (ARG1,ARG2)
F1 ([1,2,3],{' a ': "A", ' B ': "B"})
Python Basics-function advanced and adorner