There's two different things I'm trying to figure out how to do with Python.

First, when I want my program to go through a function, which then moves on to another function..... so say I start with main(). I have variables A and B. I go on to function Do_This(), which doesnt use variables A and B at all. However, the function Do_This() calls for Do_This2(), which DOES need variables A and B. The only way I know how to still have variables A and B stored somewhere is by inputting them into Do_This() with Do_This(A, B) and then inputting them into Do_This2() with Do_This2(A, B). I find this very tedious because eventually, my functions get cluttered with all these required inputs that I need in order to import them to another part of the program. How can I store a variable so it can be accessed in any function without having to import them all over the place?

The other thing I need to figure out is how to create a loop that will produce array variables (you know, like X[1] type variables) for each iteration. So say the number of iterations in the loop is three. I would then want the loop to somehow do Array1 = ["."], Array2 = ["."], and Array3 = ["."]. And if the iterations are, say, 6, it produces up to Array6. And, if I do this, how am I going to reference all these arrays later on without even knowing how many there are ahead of time?

I just need to be pointed in the right direction. This stuff's hard to explain so feel free to ask for any clarification. Thankyou!!

Recommended Answers

All 3 Replies

For the first part, use a class and declare variables that are global within the class. Classes are included in Python for a reason. You will want to use a class for programs that go beyond one simple function. For example:

class TestClass:
   def __init__(self, input_a):
      self.A=input_a     ##   global
      self.B=0     ##   global
      self.change_1()
      print self.B, self.A
      self.change_2()
      print self.B, self.A

      self.test_list=[]
      self.add_to_list(random.randrange(0,10))
      self.add_to_list(random.randrange(0,10))
      print "test_list has %d elements" % (len(self.test_list))


   def add_to_list(self, num):
      for j in xrange(0, num):
         self.test_list.append("")

   def change_1(self):
      self.A += " Change 1"
      self.B += 1


   def change_2(self):
      self.A += " & Change 2"
      self.B += 2

if __name__ == "__main__":
   empty_string=""
   TC1=TestClass(empty_string)
   empty_string="not empty ??"
   TC2=TestClass(empty_string)

I concur with woooee's suggestion to look into classes. But it would be remiss not to mention global variables, which are useful in certain simple cases.

A = 602
B = 47
def this2():
    global A,B
    A += B

def this():
    this2()

def main():
    global A
    this()
    print A  # 649

In your array question, have you considered using arrays of arrays, instead of having to name each variable? I.e., A = [ ['F'], ['o'], ['o'], ['B'], ['a'], ['r'] ] . It's usually bad practice to develop code that doesn't know variable names ahead of time. At least, at this stage of the game.

You can encapsulate your array variables in a dictionary.

>>> dd = {}
>>> for i in range(6):
... 	dd['Array%s' % i] = ['-']
... 	
>>> dd
{'Array3': ['-'], 'Array2': ['-'], 'Array1': ['-'], 'Array0': ['-'], 'Array5': ['-'], 'Array4': ['-']}
>>> keys = dd.keys()
>>> keys.sort()
>>> for key in keys:
... 	print key, dd[key]
... 	
Array0 ['-']
Array1 ['-']
Array2 ['-']
Array3 ['-']
Array4 ['-']
Array5 ['-']
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.