"Python" * functions: Global local variables, internal functions, closures

Source: Internet
Author: User
Tags closure

Functions and Procedures


Most languages will have the following definitions:

Function: Has return value

Process (Procedure): A special function that is simply special and has no return value


Only functions in Python have no procedures.

The function returns the value if it has return, and none returns if no return is returned.


Description:hello () has no return, so temp has no value. Does not display does not represent no return value. Even if hello () does not return, a None object is returned. So functions in Python have return values.


The Python function return value will dynamically determine the type automatically.

Python has no variables, only names. In other words, including the return value of a Python function does not concern the data type.

>>> def back ():
return[1, ' haha ', 4.6435]
>>> back ()
[1, ' haha ', 4.6435]


>>> def back ():
Return 1, ' haha ', 4.6435
>>> back ()
(1, ' haha ', 4.6435)

Note: The key to a tuple is a comma, not a parenthesis


ii. scope of function variables: visibility of variables

1. Local Variables

The parameters defined in the function and the variables are local variables that are not available outside the function

2. Global variable

The function defines the variable in the entire code snippet, the entire file, the entire module. Global variables can also be used in functions.


Note: If you try to modify a global variable within a function, Python automatically creates a new local variable instead of the same name as the global variable. But two variables are actually two discrete variables with different storage space. Python automatically masks the newly created local variables and protects the global variables. so do not easily modify the global variables in the function, you can access. Use caution for global variables.


Classic Examples:


Description: A is a global variable outside the function, and a within the function is a newly created local variable with the same name. A in return a has no initial value for the newly created a,a, so there must be an error returning.


If you want to use local variables inside functions outside of a function, simply add the global keyword to the function's internal definition variable.


three, inline functions and closures

1, inline function (internal function): allow to create another function inside the function


Description: The entire scope of internal functions within the external function, the definition and call of internal functions within the external function, outside the external function, there is no team fun2 call


2. Closure: An important grammatical structure of functional programming

Programming Paradigm: borrowing the terminology of philosophy, if every programmer is creating a virtual world, then the programming paradigm is the world view and methodology in which they consciously and unconsciously adopt.

Object oriented and process oriented. L

functional Programming: Baidu

closures: defined in terms of representation, if a variable in an external scope (but not in the global scope) is referenced in an intrinsic function, then the intrinsic function is considered a closure.


NOTE: Internal functions cannot be invoked outside of external functions

The local variable of the external function and the local variable of the internal function are just like the global variables for the local variables in the function. So at this point the internal function can access the local variable in the external function, but it cannot be modified.


Description: This internal function creates a new variable with the same name that has no initial value, so the return will cause an error. Because the variables outside of the intrinsic function have been blocked off.


-python3 before: Unable to resolve, only indirectly through the container type. The container type is present inside the stack, and the variable is not blocked off.

Container type: list, tuple, anything can be thrown in.


-python3: Declaring that X is not a local variable with the nonlocal keyword



Related Article

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.