Questions on How to Perform Certain Concepts

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 1
Reputation: Fractilion is an unknown quantity at this point 
Solved Threads: 0
Fractilion Fractilion is offline Offline
Newbie Poster

Questions on How to Perform Certain Concepts

 
0
  #1
Nov 27th, 2008
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!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Questions on How to Perform Certain Concepts

 
0
  #2
Nov 27th, 2008
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:
  1. class TestClass:
  2. def __init__(self, input_a):
  3. self.A=input_a ## global
  4. self.B=0 ## global
  5. self.change_1()
  6. print self.B, self.A
  7. self.change_2()
  8. print self.B, self.A
  9.  
  10. self.test_list=[]
  11. self.add_to_list(random.randrange(0,10))
  12. self.add_to_list(random.randrange(0,10))
  13. print "test_list has %d elements" % (len(self.test_list))
  14.  
  15.  
  16. def add_to_list(self, num):
  17. for j in xrange(0, num):
  18. self.test_list.append("")
  19.  
  20. def change_1(self):
  21. self.A += " Change 1"
  22. self.B += 1
  23.  
  24.  
  25. def change_2(self):
  26. self.A += " & Change 2"
  27. self.B += 2
  28.  
  29. if __name__ == "__main__":
  30. empty_string=""
  31. TC1=TestClass(empty_string)
  32. empty_string="not empty ??"
  33. TC2=TestClass(empty_string)
Last edited by woooee; Nov 27th, 2008 at 9:06 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Questions on How to Perform Certain Concepts

 
0
  #3
Nov 28th, 2008
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.
  1. A = 602
  2. B = 47
  3. def this2():
  4. global A,B
  5. A += B
  6.  
  7. def this():
  8. this2()
  9.  
  10. def main():
  11. global A
  12. this()
  13. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Questions on How to Perform Certain Concepts

 
0
  #4
Nov 28th, 2008
You can encapsulate your array variables in a dictionary.
  1. >>> dd = {}
  2. >>> for i in range(6):
  3. ... dd['Array%s' % i] = ['-']
  4. ...
  5. >>> dd
  6. {'Array3': ['-'], 'Array2': ['-'], 'Array1': ['-'], 'Array0': ['-'], 'Array5': ['-'], 'Array4': ['-']}
  7. >>> keys = dd.keys()
  8. >>> keys.sort()
  9. >>> for key in keys:
  10. ... print key, dd[key]
  11. ...
  12. Array0 ['-']
  13. Array1 ['-']
  14. Array2 ['-']
  15. Array3 ['-']
  16. Array4 ['-']
  17. Array5 ['-']
  18. >>>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 306 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC