Python zero-basic entry-7 variables and built-in functions

Source: Internet
Author: User
This blog is mainly about global variables and local variables, as well as built-in functions and parameters between built-in functions and external functions. that is to say, today's focus is on variables in functions. 1. global variables and local variables
These two variables are the same as those in other languages. In short, global variables are variables that can be used in the entire code. Their scope is the entire function, while local variables have limited scope, it is usually in a code area.
It is worth noting that if you only call the value of a global variable in a function without changing its value, it is completely correct, however, if you want to change the value of the global variable in the function without special processing, in this case, the Python language automatically generates a local variable with the same name as the called global variable, that is, shielding the global variable, operations on this variable do not affect the value of the global variable. (Although it seems to have changed)
For example, in the following program, although the count value is changed in the function, the count value printed in the function is still 5.

count=5def Myfun():     count=10     print(count)Myfun()print(count)

If you want to modify the value of a global variable within the function, you can use the global keyword in the function to modify the variable. This means that the operation is performed on the global variable, instead of generating a local variable that is the same as the global variable.

Print ("===== variables after using global =====") count = 5def Myfun (): global count # The Declaration and assignment cannot be carried out by count = 10 print (count) Myfun () print (count)

2. embedded (internal) functions
In short, an embedded function is a function defined inside the function.
It is worth noting that the call of internal functions can only be called outside of its external functions. That is to say, who has the right to use it internally.

Print ("===== internal function usage =====") def fun1 (): print ("fun1 () being called ") def fun2 (): print ("fun2 () is being called") fun2 () fun1 ()

3. closure
A closure is a parameter for an embedded function to call its external function.
This type of function requires special attention when calling it.

def funX(x):     def funY(y):          return x*y     return funYprint(funX(5)(8))

4. variable issues in closures
The following code is executed and an error is reported. Because the external function parameters are called Inside the embedded function, and the parameter x is a global variable for the function Fun2 (), function errors are caused by shielding.

def Fun1():     x=5     def Fun2():          x*=x          return x     return Fun2()#Fun1()

Second, use the nonlocal keyword to solve the problem.

# One solution is to use non-stack storage and sequence to store def Fun1 (): x = [5] def Fun2 (): x [0] * = x [0] return x [0] return Fun2 () print (Fun1 (), "\ n ") # One solution is to use the nonlocal keyword def Fun3 (): x = 5 def Fun4 (): nonlocal x * = x return Fun4 () print (Fun3 ())

Variables in functions need to be carefully considered. after all, they are somewhat different from those learned before.

The above are the seven variables and built-in functions for getting started with Python. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.