Python - 全局变量斗争
Python - Global variable struggle
这可能很荒谬,但我无法理解 Python 中全局变量的使用。使用关键字
会引发语法错误
global
尽管如此,我在文档中正是按照这种方式查找的。
我知道代码重复使用全局变量很笨拙。无论如何,如何正确使用它们?
import random
r_list = []
my_list =[3,4,45,7,23,33]
match_list=[]
iteration = 0
number_matches = 0
def fill_random_list():
for i in range(7):
# print (random.randint(1,49))
r_list.append(random.randint(1,49))
def return_matches(lista, listb):
# print set(lista).intersection(listb)
return set(lista).intersection(listb)
def return_number_matches(l_matches):
if l_matches:
return len(l_matches)
def draw_lottery():
while global number_matches < 5:'''
File "C:/Lottery.py", line 27
while global number_matches < 5:
^
SyntaxError: invalid syntax'''
global r_list = []
global match_list = []
fill_random_list()
match_list=return_matches(r_list, my_list)
global number_matches=(return_number_matches(global match_list))
global iteration+=1
if number_matches > 4:
print (str(global iteration) + ';' + str(global number_matches))
def iterate(number):
for i in enumerate(range(number),1):
print('Round: ' + str(i[0]))
draw_lottery()
def main():
iterate(10)
if __name__ == "__main__":
main()
你的代码问题很简单
如果你想在一个函数中修改一个全局变量,你首先需要明确地引用它,怎么做?
包括行
global variable_name
在您的函数代码中,这应该发生在读取或修改此变量之前
不要在每次使用全局变量时都使用 global
进行注释。您需要做的就是在修改变量之前在您的函数中写入 global var1, var2
。
如果您只是阅读它们,则不需要使用 global
。只有在分配给他们时才需要它。
def draw_lottery():
global number_matches, r_list, match_list, number_matches, iteration
while number_matches < 5:
r_list = []
match_list = []
fill_random_list()
match_list = return_matches(r_list, my_list)
number_matches = return_number_matches(match_list)
iteration += 1
if number_matches > 4:
print(str(iteration) + ';' + str(number_matches))
作为一种良好的风格,避免使用全局变量。当许多函数共享相同的变量时,很难跟踪程序状态。最好尽可能使用参数和 return
值。
这可能很荒谬,但我无法理解 Python 中全局变量的使用。使用关键字
会引发语法错误global
尽管如此,我在文档中正是按照这种方式查找的。
我知道代码重复使用全局变量很笨拙。无论如何,如何正确使用它们?
import random
r_list = []
my_list =[3,4,45,7,23,33]
match_list=[]
iteration = 0
number_matches = 0
def fill_random_list():
for i in range(7):
# print (random.randint(1,49))
r_list.append(random.randint(1,49))
def return_matches(lista, listb):
# print set(lista).intersection(listb)
return set(lista).intersection(listb)
def return_number_matches(l_matches):
if l_matches:
return len(l_matches)
def draw_lottery():
while global number_matches < 5:'''
File "C:/Lottery.py", line 27
while global number_matches < 5:
^
SyntaxError: invalid syntax'''
global r_list = []
global match_list = []
fill_random_list()
match_list=return_matches(r_list, my_list)
global number_matches=(return_number_matches(global match_list))
global iteration+=1
if number_matches > 4:
print (str(global iteration) + ';' + str(global number_matches))
def iterate(number):
for i in enumerate(range(number),1):
print('Round: ' + str(i[0]))
draw_lottery()
def main():
iterate(10)
if __name__ == "__main__":
main()
你的代码问题很简单 如果你想在一个函数中修改一个全局变量,你首先需要明确地引用它,怎么做? 包括行
global variable_name
在您的函数代码中,这应该发生在读取或修改此变量之前
不要在每次使用全局变量时都使用 global
进行注释。您需要做的就是在修改变量之前在您的函数中写入 global var1, var2
。
如果您只是阅读它们,则不需要使用 global
。只有在分配给他们时才需要它。
def draw_lottery():
global number_matches, r_list, match_list, number_matches, iteration
while number_matches < 5:
r_list = []
match_list = []
fill_random_list()
match_list = return_matches(r_list, my_list)
number_matches = return_number_matches(match_list)
iteration += 1
if number_matches > 4:
print(str(iteration) + ';' + str(number_matches))
作为一种良好的风格,避免使用全局变量。当许多函数共享相同的变量时,很难跟踪程序状态。最好尽可能使用参数和 return
值。