[英]Global static variables in Python
def Input():
c = raw_input ('Enter data1,data2: ')
data = c.split(',')
return data
我需要在其他函數中使用列表data
,但我不想每次都輸入raw_input
。 我如何在c ++中創建類似全局靜態的 data
並將其放在需要的任何地方?
在函數中添加一行:
def Input():
global data
c = raw_input ('Enter data1,data2: ')
data = c.split(',')
return data
global data
語句是一個使data
成為全局變量的聲明。 調用Input()
您將能夠引用其他函數中的data
。
使用全局變量通常被認為是不好的做法。 最好使用適當的面向對象並將“數據”包裝在適當的類/對象中,例如
class Questionaire(object):
def __init__(self):
self.data = ''
def input(self):
c = raw_input('Enter data1, data2:')
self.data = c.split(',')
def results(self):
print "You entered", self.data
q = Questionaire()
q.input()
q.results()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.