function advanced for Python basics

Source: Internet
Author: User

If there is a function, the implementation returns a larger value in two numbers:

def my_max (x, y):    m = x if x>y else y    return m
Bigger = My_max (10,20)
Print (bigger)

Before I told you to return the results back, did you do it? But have you ever wondered why we should return the results? If we do not return m, print directly in the program, OK?

To see the results:

>>> def my_max (x, y): ...     m = x if x>y else y ... >>> my_max (10,20) >>> print (m) Traceback (most recent call last):  File "< Stdin> ", line 1, in <module>nameerror:name ' m ' are not defined

The error! The error is "name ' m ' isn't defined". Variable m is not defined ... Why? I clearly defined it!

Here we begin by remembering how the Python code runs when it encounters a function.

After the Python interpreter starts executing, it opens up a space in memory

Whenever a variable is encountered, the corresponding relationship between the variable name and the value is recorded.

But when a function definition is encountered, the interpreter simply reads the function name into memory , indicating that the function exists, and that the variables inside the function and the logic interpreter do not care at all.

When executing to a function call, the Python interpreter will open up a memory to store the contents of the function , at which point the variables in the function are stored in the newly opened memory. The variables in the function can only be used inside the function, and all the contents of this memory will be emptied as the function executes.

We give a name to the space that "holds the relationship between names and values"-called namespaces

At the beginning of the code, the space created to store the "variable name-value relationship" is called the global namespace , and the temporary space opened in the function's run is called the local namespace .

Namespaces and Scopes

As mentioned in the Zen of Python: Namespaces are a great idea, let's use it to the fullest!

The namespaces are divided into three types:

Global namespaces

Local namespaces

Built-in namespaces

* The built-in namespace holds the Python interpreter's name for us: Input,print,str,list,tuple ... They are all familiar to us and can be used in a way.

Order of loading and taking values between three namespaces:

Load order: Built-in namespaces (pre-Program load)-Global namespaces (program run: Load from top to bottom) local namespaces (program run: Load only when called)

Value:

Built-in namespaces, local namespaces, and global namespaces, local call spaces

Local use of variable values

x = 1def F (x):    print (x) print (10)

Global invocation: Built-in namespaces, global namespaces

In global reference variable X
x = 1
def f (x):    print (x) f (Ten) print (x)

In the global reference built-in Max
Print (max)

Scope

Scope is the scope of action, according to the effective scope can be divided into global scope and local scope.

Global scope: Contains built-in namespaces, global namespaces , can be referenced anywhere throughout the file, are globally valid

Local scope: Local namespace, only valid locally

Globals and Locals methods

Globals and locals are called globally

Print (Globals ()) print (Locals ())

Globals and locals are called locally
def func ():
    A =    b = Print (    locals ())    print (Globals ()) func ()

Global keyword
A = 10def func ():    global a    = 20print (a) func () print (a)

Nested and scoped chains of functions

Nested calls to functions

def max2 (x, y):    m  = x if x>y else y    return mdef max4 (a,b,c,d):    res1 = Max2 (A, b)    res2 = Max2 (res1 , c)    Res3 = Max2 (res2,d)    return res3# max4 (23,-7,31,11)

Nested Definitions of functions

Def f1 ():    print ("in F1")    def f2 ():        print ("in F2")    F2 () F1 ()
Def f1 ():    def f2 ():        def f3 ():            print ("In-F3") Print ("In-        F2")        F3 ()    print ("in F1")    F2 ()    F1 ()

function advanced for Python basics

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.