| | |
Questions on How to Perform Certain Concepts
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
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!!
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!!
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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:
Python Syntax (Toggle Plain Text)
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)
Last edited by woooee; Nov 27th, 2008 at 9:06 pm.
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.
In your array question, have you considered using arrays of arrays, instead of having to name each variable? I.e.,
Python Syntax (Toggle Plain Text)
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. •
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
You can encapsulate your array variables in a dictionary.
Python Syntax (Toggle Plain Text)
>>> 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 ['-'] >>>
![]() |
Similar Threads
- Programming FAQ - Updated 1/March/2005 (Computer Science)
- C++ Books (C++)
- Several C++ Designing Questions (C++)
- Enter string to the next line (C++)
Other Threads in the Python Forum
- Previous Thread: Text Copy in HtmlWindow
- Next Thread: Computer picks a random word and player has to guess it.
Views: 306 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






