Small ape circle python learning - function return value and scope

External function code in order to obtain the execution result of the function, you can put the results returned by the return statement in a function

def stu_register(name, age, course='PY' ,country='CN'):

    print ( "---- ------ registered student information")

    print ( "Name:", name)

    print("age:", age)

    print ( "Nationality:", country)

    print ( "Course:", course)

    if age > 22:

        return False

    else:

        return True

registriation_status = stu_register ( "King mountain artillery", 22, course = "PY whole stack development", country = 'JP')

if registriation_status:

    print ( "Registration Success")

else:

    print("too old to be a student.")

note

Function encountered in the implementation process as long as the return statement stops execution and returns the result, so can also be understood as representing the end of the function return statement

If the function return is not specified, then the function returns None

Global and local variables

name = "Alex Li"

def change_name():

    name = "King of the Golden Horn, a senior Tesla Cock wire"

    print("after change", name)

change_name()

print ( "name on the outside to see what changed?", name)

Export

after change King of the Golden Horn, a senior Tesla Cock wire

In the name changed outside to look at it? Alex Li

Why change the value inside the function name, and print out when they did not change it? Because these two name not the same thing

Variables defined in a local variable called the function, the variable a defined at the beginning of the procedure called global variables.

Global variable scope (i.e. the effective range) is the entire program, the local variable is a function of the variable scope of the definition.

Find order of the variables are local variables> Global Variables

When a global variable and a local variable with the same name in the function definition of local variables, local variables play a role; global variables to function elsewhere.

In the function is not directly modify global variables

Just want to modify global variables in a function how to do?

name = "Alex Li"

def change_name():

    global name # declare a global variable

    name = "Alex King, also known as the Golden Horn, love life, love of freedom, love the girl."

    print("after change", name)

change_name()

print ( "name on the outside to see what changed?", name)

global name effect is to declare global variables in a function name, which means the top name = "Alex Li" even if not written, the rearmost print program can also print name

Transfer list, the phenomenon of dictionaries, collections generated

d = {"name":"Alex","age":26,"hobbie":"大保健"}

l = ["Rebeeca","Katrina","Rachel"]

def change_data(info,girls):

    info [ "hobbie"] = "learning"

    girls.append("XiaoYun")

change_data(d,l)

print(d,l)

Results of { 'name': 'Alex', 'age': 26, 'hobbie': 'Learning'} [ 'Rebeeca', 'Katrina', 'Rachel', 'XiaoYun']

Not to say that can not change the global variables in a function it, how it changed?

According to the figure we can see that this procedure is the d dict memory address passed change_data function, compared to dict tank, inside k, v compared tank filled with fish. Now just threw the fish tank function, you can not change the tank itself, but there may be fish. Equivalent to only one pass on this relationship to d reference to the function parameter. The idea is to reduce the waste of memory, because if this relatively large dict, once passed to the function will copy in a new value, the efficiency is too low.

Reproduced in: https: //www.jianshu.com/p/1eb0b8386527

Guess you like

Origin blog.csdn.net/weixin_33733810/article/details/91100195