程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Advanced Series (6)

編輯:Python

Global and Return

You may have met , python Some functions in have one at the end return keyword . Do you know what it is ? It and other languages return similar . Let's check this little function :

def add(value1, value2):
return value1 + value2
result = add(3, 5)
print(result)
# Output: 8

The above function takes two values as input , Then output the sum of them . We can do the same thing :

def add(value1,value2):
    global result
    result = value1 + value2
add(3,5)
print(result)
# Output: 8

First of all, let's talk about the first paragraph, which contains return Keyword code . That function assigns a value to the variable that calls it ( In this case result Variable ).

In most cases , You don't need to use global keyword . However, let's also examine the next paragraph, which contains global Keyword code . That function generates a global( overall situation ) Variable result.

global What does it mean here ?global Variable means that we can access this variable outside of the function . Let's prove it with an example :

First , It's not used global Variable

def add(value1, value2):
    result = value1 + value2
add(2, 4)
print(result)

 Oh A bad , We encountered an anomaly . Why is that? ?python The interpreter reports an error saying that there is no one named result The variable of .

This is because result A variable can only be accessed inside the function that created it , Unless it is global (global).

Traceback (most recent call last):

File "", line 1, in result

NameError: name 'result' is not defined

  Now we run the same code , It's just that result The variable is set to global after

def add(value1, value2):
    global result
    result = value1 + value2
add(2, 4)
print(result)
#6

As we wish , There are no exceptions in the second run . In actual programming , You should try to avoid global Turn off

Key word , It only makes life difficult , Because it introduces extra variables into the global scope .

Multiple return value

What if you want to return two variables from a function instead of one ?

Novices have several ways . The most famous method , It's using global keyword . Let's take a look at this useless example :

def profile():
    global name
    global age
    name = "Danny"
    age = 30
profile()
print(name)
# Output: Danny
print(age)
# Output: 30

Be careful : Do not try to use the above methods . Important things are to be repeated for 3 times , Do not try to use the above methods !

Some people try at the end of a function , Returns a that contains multiple values tuple( Tuples ),list( list ) or

person dict( Dictionaries ), To solve this problem . This is a feasible way , And it works like a dark magic :

def profile():
    name = "Danny"
    age = 30
    return (name, age)
profile_data = profile()
print(profile_data[0])
# Output: Danny
print(profile_data[1])
# Output: 30

Or follow the more common conventions :

def profile():
    name = "Danny"
    age = 30
    return name, age

This is a better way than lists and dictionaries . Do not use global keyword , Unless you know what you're doing .global Maybe it's a better choice in some scenarios ( But most of them are not ).


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved