If you have any questions about Python functions, for example, if you have doubts about the local variables of Python functions, you can read our article. I hope you will gain some benefits after reading our article, the following is a detailed description of the article.
Local variable
When you declare variables in the function definition, they have no relationship with other variables with the same name outside the function, that is, the variable name is local for the function. This is called the scope of a variable. The scope of all variables is the block they are defined, starting from the point where their names are defined.
Use a local variable example 7.3 using a Python Function
How to output Python Functions
When we use the value of x for the first time in a function, Python uses the value of the form parameter declared by the function. Next, I
- #!/usr/bin/python
- # Filename: func_local.py
- def func(x):
- print 'x is', x
- x = 2
- print 'Changed local x to', x
- x = 50
- func(x)
- print 'x is still', x
-
Value 2 is assigned to x. X is the local variable of the function. Therefore, when we change the value of x in the function, the x defined in the main block is not affected. In the last print statement, we prove that the value of x in the main block is indeed not affected.
The above section describes the Python functions in practical application.