Global Static Variables in Python

def Input(): c = raw_input ('Enter data1,data2: ') data = c.split(',') return data 

I need to use the data list in other functions, but I don't want to enter raw_input every time. How can I make data as global static in C ++ and place it wherever it is needed?

+4
source share
2 answers

Add one line to your function:

 def Input(): global data c = raw_input ('Enter data1,data2: ') data = c.split(',') return data 

The global data statement is an declaration that makes data global variable. After calling Input() you can access data in other functions.

+12
source

using global variables is usually considered bad practice. It is better to use the correct orientation of the objects and wrap the "data" in the proper class / object, for example.

 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() 
+4
source

All Articles