Re: [Tutor] How to convert seconds to hh:mm:ss format
On Fri, Feb 17, 2012 at 3:16 AM, alain Delon racg...@yahoo.com wrote: userName = raw_input(Enter your name ) print hi + userName + . \n seconds = input(Enter the number of seconds since midnight:) hours = seconds/3600 hoursRemain = hours%60 Here is your error minutes = hoursReamain/60 secondsRemain = minutes%60 print hours: + minutes: + seconds I'm going to be crazy. Please give me a hand. Thank you very much. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging While Loops for Control
On 17/02/12 03:27, Luke Thomas Mergner wrote: In the meantime, and continuing my problem of over-cleverness, At least you know what your problem is :-) Bonus question: when I create a the def score(self) in class Hand, should that be an generator? No. And if so where do I go as a newb to understand generators? The documentation. Or Google python generator tutorial howto I'm really not understanding them yet. You don't need to for what you are doing. The x for x in y: syntax makes it harder to follow for learners, Read about list comprehensions first. It helps if you studied sets in math at school. The format is somewhat like the math notation for defining a set. But FWIW it took me a long time to get used to that syntax too. Some thoughts class Card(object): def __init__(self): self.score = self.deal() def deal(self): deal a card from 1 to 52 and return it's points return self.getValue(int(math.floor(random.uniform(1, 52 I think you only need random.randint(1,52) here. It simplifies it quite a lot! But you still have the problem that you might wind up with two identical cards! It might be better to create a deck(list) of 52 cards, shuffle() them and then pop the value off of that deck. def getValue(self, card): Converts the values 1 - 52 into a 1 - 13 and returns the correct blackjack score based on remainder. if (card % 13 == 0 or card % 13 == 11 or card % 13 == 12): #Face Cards are 10 points return 10 elif (card % 13 == 1): return 11 else: #Regular cards, return their value return card % 13 Just calculate the value once rather than doing a mod division each time. Much more efficient and easier to read. def showCard(self): return repr(self.score) Why are you using repr? Why not just return the integer value? class Hand: def __init__(self): self.cards = [] #Add cards this way to avoid duplicates. for i in range(2): self.cards.append(Card()) What makes you think it will avoid duplicates? The card values are generated at random. def hit(self): self.cards.append(Card()) def showHand(self): return self This seems fairly pointless. If i already have a reference to an object why would I call a method that returns what I already have? Maybe returning self.cards would make sense. But really the Hand class should do whatever is needed to cards so even that doesn't make sense. The only thing this could usefully do is pretty print the cards in a formatted string. def score(self): #how do you sum(objects) ??? You could define an __add__() method on your cards so that Card()+Card() returns an integer. (Or you could use a generator here if you really, really wanted to. But the __add__ method is cleaner IMHO) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] dict vs several variables?
I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/cleaner to just build a global dict and have everything go into it or pass multiple arguments to each function and have it return the calculated value? Thanks! Leam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to hide cursor in Terminal?
I made a timer that counts down from five minutes. This code runs fine but I a seeing a cursor blinking on the first number as the code is running. How do I avoid this? I am using gnome-terminal and Python 2.6.6. #!/usr/bin/python import time import sys import os def countd(): seconds = 59 minutes = 4 five_minutes = 0 os.system('clear') while five_minutes != 300: sys.stdout.write(%d:%02.f\r % (minutes, seconds)) sys.stdout.flush() seconds -= 1 if seconds == -1: minutes -= 1 seconds = 59 five_minutes += 1 time.sleep(1) countd() Brandon ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding a maximum between the absolute difference of several columns
Andre' Walker-Loud wrote: import numpy as np Vhel_fdiff3 = np.array([abs(Vmatch3_1 - Vmatch3_2), abs(Vmatch3_1 - Vmatch3_3), abs(Vmatch3_3 - Vmatch3_2)]) your_answer = Vhel_fdiff3.max(axis=0) or import numpy as np a = np.array([Vmatch3_1-Vmatch3_2, Vmatch3_1-Vmatch3_3, Vmatch3_3- Vmatch3_2]) print np.max(np.abs(a), axis=0) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
Leam Hall wrote: I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/cleaner to just build a global dict and have everything go into it or pass multiple arguments to each function and have it return the calculated value? The latter. It makes the dependencies explicit to a reader of the function, it simplifies unit tests, allows it to reuse functions in a different context, and it is more likely to work in a multi-threaded environment. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to hide cursor in Terminal?
On Fri, Feb 17, 2012 at 12:38 PM, brandon w bbbggg...@gmail.com wrote: I made a timer that counts down from five minutes. This code runs fine but I a seeing a cursor blinking on the first number as the code is running. How do I avoid this? I am using gnome-terminal and Python 2.6.6. #!/usr/bin/python import time import sys import os def countd(): seconds = 59 minutes = 4 five_minutes = 0 os.system('clear') while five_minutes != 300: sys.stdout.write(%d:%02.f\r % (minutes, seconds)) sys.stdout.flush() seconds -= 1 if seconds == -1: minutes -= 1 seconds = 59 five_minutes += 1 time.sleep(1) countd() Brandon I believe that the curses[1] library can do this, though that may be somewhat complex (curses is essentially a GUI toolkit for the terminal). You should probably read up on the tutorial mentioned there. HTH, Hugo [1] http://docs.python.org/library/curses.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
Thanks Peter! My concern with variables is that they have to be passed in specific order to the function, and they may have to have their type set multiple times so that you can perform the right functions on them. In a dict you could set it on insert and not have to worry about it. Thanks! Leam On 2/17/12, Peter Otten __pete...@web.de wrote: Leam Hall wrote: I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/cleaner to just build a global dict and have everything go into it or pass multiple arguments to each function and have it return the calculated value? The latter. It makes the dependencies explicit to a reader of the function, it simplifies unit tests, allows it to reuse functions in a different context, and it is more likely to work in a multi-threaded environment. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Mind on a Mission http://leamhall.blogspot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
On 17 February 2012 14:04, leam hall leamh...@gmail.com wrote: My concern with variables is that they have to be passed in specific order to the function, and they may have to have their type set multiple times so that you can perform the right functions on them. In a dict you could set it on insert and not have to worry about it. There is **kwargs which is behaves like a dict. This is very powerful but comes with the drawback that you have to make sure you get all the variables you need for your function to work properly. I found [1] that is a decent explanation of how these work. Other might have better explanations. Greets Sander [1] http://basicpython.com/understanding-arguments-args-and-kwargs-in-python/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
On 2/17/12, Peter Otten__pete...@web.de wrote: Leam Hall wrote: I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/cleaner to just build a global dict and have everything go into it or pass multiple arguments to each function and have it return the calculated value? The latter. It makes the dependencies explicit to a reader of the function, it simplifies unit tests, allows it to reuse functions in a different context, and it is more likely to work in a multi-threaded environment. On 02/17/2012 08:04 AM, leam hall wrote: Thanks Peter! My concern with variables is that they have to be passed in specific order to the function, and they may have to have their type set multiple times so that you can perform the right functions on them. In a dict you could set it on insert and not have to worry about it. Thanks! Leam Please don't top-post. Put your remarks after the parts you're quoting. You're asking for best-practice, presumably as part of your transition from small snippets to larger scale. First habit that beginners have to break is the tendency to keep things in globals. Can it make things easier? Yes, until you try to return to that code and make changes, and discover that one change affects an unknown number of other parts. Yes, until you decide you want to use one of those functions in a multithreaded environment. Yes, until somebody else has to use your code. Variable can't get their type set. They are bound at any moment to an object, and that object has a type. Period. Passed in a specific order? Of course. Very few functions, even binary ones, are commutative on their arguments. Do you expect divide(7, 42) to give the same answer as divide(42, 7) ? You can also use keyword arguments to help disambiguate the order of arguments. That's especially useful when some arguments are optional. Real question is whether some (seldom all) of those variables are in fact part of a larger concept. If so, it makes sense to define a class for them, and pass around objects of that class. Notice it's not global, it's still passed as an argument. This can reduce your parameters from 20 to maybe 6. But make sure that the things the class represents are really related. Dictionaries are a built-in collection class, as are lists, sets, and tuples. But you can write your own. An example of needing a class might be to hold the coordinates of a point in space. You make a Location class, instantiate it with three arguments, and use that instance for functions like move_ship(ship, newlocation) There are times to have arbitrary lists or dictionaries to pass into a function, and Python has added syntax (*args and **kwargs) to make that convenient. But the times that is needed are very specialized. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
leam hall wrote: My concern with variables is that they have to be passed in specific order to the function, Yes, unless you use keywords. You can invoke def div(x, y): return x // y a = div(3, 2) b = div(y=3, x=2) assert a == b and they may have to have their type set I have no idea what you mean by have their type set. Can you give an example? multiple times so that you can perform the right functions on them. In a dict you could set it on insert and not have to worry about it. Instead you'll have to worry about the contents of the dict which I suspect will be even harder to verify in a non-trivial script. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
On 2/17/12, Dave Angel d...@davea.name wrote: Real question is whether some (seldom all) of those variables are in fact part of a larger concept. If so, it makes sense to define a class for them, and pass around objects of that class. Notice it's not global, it's still passed as an argument. This can reduce your parameters from 20 to maybe 6. But make sure that the things the class represents are really related. Dictionaries are a built-in collection class, as are lists, sets, and tuples. But you can write your own. An example of needing a class might be to hold the coordinates of a point in space. You make a Location class, instantiate it with three arguments, and use that instance for functions like move_ship(ship, newlocation) DaveA Understood. In this case, the first half dozen variables are input and the rest are derived from the first ones. A class might make sense and though I understand them a little, not enough to make a good judgement on it. The task is to take parameters for a scuba dive; depth, gas mix, time, air consumption rate, and compute the O2 load, gas required, etc. Leam -- Mind on a Mission http://leamhall.blogspot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
On 2/17/12, Peter Otten __pete...@web.de wrote: leam hall wrote: and they may have to have their type set I have no idea what you mean by have their type set. Can you give an example? Peter, The variables input seem to be assumed to be strings and I need them to be an integer or a float most of the time. Doing simple math on them. Thanks! Leam -- Mind on a Mission http://leamhall.blogspot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
On 02/17/2012 09:06 AM, leam hall wrote: On 2/17/12, Dave Angeld...@davea.name wrote: Real question is whether some (seldom all) of those variables are in fact part of a larger concept. If so, it makes sense to define a class for them, and pass around objects of that class. Notice it's not global, it's still passed as an argument. This can reduce your parameters from 20 to maybe 6. But make sure that the things the class represents are really related. Dictionaries are a built-in collection class, as are lists, sets, and tuples. But you can write your own. An example of needing a class might be to hold the coordinates of a point in space. You make a Location class, instantiate it with three arguments, and use that instance for functions like move_ship(ship, newlocation) DaveA Understood. In this case, the first half dozen variables are input and the rest are derived from the first ones. A class might make sense and though I understand them a little, not enough to make a good judgement on it. The task is to take parameters for a scuba dive; depth, gas mix, time, air consumption rate, and compute the O2 load, gas required, etc. Leam There are two ways to think of a class. One is to hold various related data, and the other is to do operations on that data. If you just consider the first, then you could use a class like a dictionary whose keys are fixed (known at compile time). class MyDiver(object): def __init__(self, depth, gasmix, time, rate): self.depth = int(depth) self.gasmix = int(gasmix) self.time = datetime.datetime(time) self.rate = float(rate) Now if you want to use one such: sam = MyDiver(200, 20, 04/14/2011, 3.7) bill = MyDiver(.) And if you want to fetch various items, you'd do something like: if sam.depth bill.depth instead of using sam[depth] and bill[depth] Next thing is to decide if the functions you're describing are really just methods on the class class MyDiver(object): def __init__( ... as before) def get_load(self): return self.gasmix/self.rate(or whatever) and used as print sam.get_load() that last could be simplified with a decorator @property def load(self): return self.gasmix/self.rate now it's used as though it's a regular data attribute print sam.load -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
leam hall wrote: On 2/17/12, Peter Otten __pete...@web.de wrote: leam hall wrote: and they may have to have their type set I have no idea what you mean by have their type set. Can you give an example? Peter, The variables input seem to be assumed to be strings and I need them to be an integer or a float most of the time. Doing simple math on them. If you are processing user input you should convert that once no matter what the structure of your program is. Example: #WRONG, integer conversion in many places in the code def sum(a, b): return int(a) + int(b) def difference(a, b): return int(a) - int(b) a = raw_input(a ) b = raw_input(b ) print a + b =, sum(a, b) print a - b =, difference(a, b) #BETTER, integer conversion in a single place def sum(a, b): return a + b def difference(a, b): return a - b def get_int(prompt): return int(raw_input(prompt)) a = get_int(a ) b = get_int(b ) print a + b =, sum(a, b) print a - b =, difference(a, b) The second form has the added benefit that you can easily make get_int() more forgiving (allow the user to try again when his input is not a valid integer) or make other changes to the code (e. g. allow floating point input). ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding a maximum between the absolute difference of several columns
import numpy as np Vhel_fdiff3 = np.array([abs(Vmatch3_1 - Vmatch3_2), abs(Vmatch3_1 - Vmatch3_3), abs(Vmatch3_3 - Vmatch3_2)]) your_answer = Vhel_fdiff3.max(axis=0) or import numpy as np a = np.array([Vmatch3_1-Vmatch3_2, Vmatch3_1-Vmatch3_3, Vmatch3_3- Vmatch3_2]) print np.max(np.abs(a), axis=0) yes, this one is better than mine. It is more flexible, leaving more options for what you may want to do with it afterwards, andre ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging While Loops for Control
class Card(object): def __init__(self): self.score = self.deal() def deal(self): deal a card from 1 to 52 and return it's points return self.getValue(int(math.floor(random.uniform(1, 52 I think you only need random.randint(1,52) here. It simplifies it quite a lot! But you still have the problem that you might wind up with two identical cards! It might be better to create a deck(list) of 52 cards, shuffle() them and then pop the value off of that deck. I'd suggest using random.sample() instead, but it might just be a matter of style: from random import sample deck = range(1,53) sample(deck, 2) [43, 9] sample(deck, 2) [8, 27] It's a function a lot of people doesn't know about, for some reason. Best part is that it leaves deck (or whatever population you're taking the sample from) alone, so you don't have to rebuild it every time you want to reset the deck. Ah, but I suppose you want to keep track of what cards are available, too. Forgot about that part. I guess you can do: sample(deck, 52) #or instead of deck range(1,53) for each new round, and pop items from the returned sample instead of popping the deck list every time. In the end I suppose it's a matter of style. Oh, and the returned list is in selection order (which might be important but maybe not in Black Jack). Maybe I should have just stayed quiet. -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging While Loops for Control
Alan Gauld wrote: The x for x in y: syntax makes it harder to follow for learners, Read about list comprehensions first. It helps if you studied sets in math at school. The format is somewhat like the math notation for defining a set. But FWIW it took me a long time to get used to that syntax too. To grok list comprehensions it helps to write the equivalent for loops first. You can mechanically convert items = [] for x in abcd: if x d: if x != b: items.append(x) to items = [x for x in abcd if x d if x != b] The for loops and ifs stay in place, the expression passed to append() in the innermost loop moves to the beginning. Another example: items = [] for x in abcd: if x d: for y in x + x.upper(): if y != A: items.append(y*2) This becomes items = [y*2 for x in abcd if x d for y in x + x.upper() if y != A] Real-world list comprehensions tend to be less complicated, but to understand them you just have to reverse the conversion: items = [y*2 for x in abcd if x d for y in x + x.upper() if y != A # append y*2 ] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
On 17/02/12 14:10, leam hall wrote: The variables input seem to be assumed to be strings They are not assumed to be strings, they *are* strings. Users can only type characters at the keyboard (the usual source of input). Your program has to interpret those characters and convert to the right type. It's good practice to do that as soon as possible which is why you often see code like: numVal = int( raw_input(Type a number: ) ) fltVal = float( raw_input(Type a number: ) ) strVal = raw_input(Type your name: ) This also has the advantage that you can catch user input errors early and request re-input. If you just store whatever the user types (four say) and then try to convert during the math you get an error far too late to fix. eg. try print pow(int(four), 2) You can convert them to strings for display later but usually you don't want to, because you will use string formatting to improve their appearance (especially with floats). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to hide cursor in Terminal?
On 17/02/12 11:38, brandon w wrote: I made a timer that counts down from five minutes. This code runs fine but I a seeing a cursor blinking on the first number as the code is running. How do I avoid this? Try putting the carriage return at the start of the line. You print the line then reset the cursor to the beginning. You want to reset the cursor then print leaving the cursor at the end of the line(I assume?) If you want to get rid of the cursor entirely then I think you might be able to do it via a set tty command or similar - but why would you?! And if you do, don't forget to reset it after you finish! while five_minutes != 300: sys.stdout.write(%d:%02.f\r % (minutes, seconds)) sys.stdout.write(\r%d:%02.f % (minutes, seconds)) That might do it for you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to hide cursor in Terminal?
This is what I ended up using: import time import sys import os def countd(): seconds = 59 minutes = 4 five_minutes = 0 os.system('clear') os.system('setterm -cursor off') while five_minutes != 300: sys.stdout.write(\r%d:%02.f\t % (minutes, seconds)) sys.stdout.flush() seconds -= 1 if seconds == -1: minutes -= 1 seconds = 59 five_minutes += 1 time.sleep(1) countd() os.system('setterm -cursor on') This worked like a charm! Thank you guys for your help! Now I'll have to see if I can get the same thing working on a Windows machine. Brandon On Fri, Feb 17, 2012 at 2:00 PM, Alan Gauld alan.ga...@btinternet.comwrote: On 17/02/12 11:38, brandon w wrote: I made a timer that counts down from five minutes. This code runs fine but I a seeing a cursor blinking on the first number as the code is running. How do I avoid this? Try putting the carriage return at the start of the line. You print the line then reset the cursor to the beginning. You want to reset the cursor then print leaving the cursor at the end of the line(I assume?) If you want to get rid of the cursor entirely then I think you might be able to do it via a set tty command or similar - but why would you?! And if you do, don't forget to reset it after you finish! while five_minutes != 300: sys.stdout.write(%d:%02.f\r % (minutes, seconds)) sys.stdout.write(\r%d:%02.f % (minutes, seconds)) That might do it for you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ __**_ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
On 02/17/2012 09:26 AM, Dave Angel wrote: There are two ways to think of a class. One is to hold various related data, and the other is to do operations on that data. If you just consider the first, then you could use a class like a dictionary whose keys are fixed (known at compile time). I think a class is the way to go for a couple reasons. First, of course, is that it pushes me to learn more. It also lets me encapsulate a lot of the work into one thing and keep the methods there. Thanks! Leam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Pyhton editor
All Couple of weeks ago I was looking for a nice free IDE for python and got many wonderful suggestion form very helpful people. However I stumbled upon PyScripter and I find it really amazing. I feel once u try it you will hook on to it for ever Thanks Best Regards Kapil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict vs several variables?
Leam Hall wrote: I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/cleaner to just build a global dict and have everything go into it or pass multiple arguments to each function and have it return the calculated value? A global dict is like the Dark Side of the Force: easier, quicker, simpler, but it leads to pain and anguish and great suffering. I assume you understand why global variables should be avoided as much as possible? E.g. # Bad! Wrong! Do not do this! x = '' y = '' z = '' def get_user_input(): global x, y x = raw_input(Please enter x: ) y = raw_input(Please enter y: ) def do_calculation(): global z z = The answer is %s % (x.upper() + y.lower()) def main() get_user_input() do_calculation() print z If you're not familiar with the reasons to avoid global variables, you should google for Global variables considered harmful, or start here: http://c2.com/cgi/wiki?GlobalVariablesAreBad Well, using a single global dict is *almost* as bad, and for most of the same reasons: # Do not do this either. values = {'x': '', 'y': '', 'z': ''} def get_user_input(values): values['x'] = raw_input(Please enter x: ) values['y'] = raw_input(Please enter y: ) def do_calculation(values): x = values['x'] y = values['y'] values['z'] = The answer is %s % (x.upper() + y.lower()) def main() get_user_input(values) do_calculation(values) print values['z'] This is a mild improvement, at least you can pass in an alternative dict if needed, but it still suffers from failure of encapsulation (all functions that have access to the dict have access to all variables, whether they need them or not) and other problems. Just about the only valid use of this pattern I can think of is for global settings that apply application-wide. Such settings tend to be write-once, which mitigates the disadvantages of global and pseudo-global variables. By the way, in case it isn't obvious, changing from a dict to a instance with named attributes values.x values.y values.z is just a cosmetic change, it doesn't change the basic problems. For a toy problem like the above, it might seem hardly worth the hassle of de-globalising the functions, but for real code this really pays off in fewer bugs and easier maintenance: def get_user_input(): x = raw_input(Please enter x: ) y = raw_input(Please enter y: ) return (x, y) def do_calculation(x, y): return The answer is %s % (x.upper() + y.lower()) def main() a, b = get_user_input() result = do_calculation(a, b) print result I assume the problem you are solving is more than just a toy. In that case, passing individual variables to only the functions that need them is a better solution. RELATED variables that MUST stay together can be collected into data structures such as tuples, lists, dicts, or custom classes. But don't be tempted to dump everything into one giant dict -- that's barely better than using globals. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pyhton editor
On Fri, Feb 17, 2012 at 10:48 PM, Kapil Shukla shukla.ka...@gmail.comwrote: All Couple of weeks ago I was looking for a nice free IDE for python and got many wonderful suggestion form very helpful people. However I stumbled upon PyScripter and I find it really amazing. I feel once u try it you will hook on to it for ever Thanks Best Regards Kapil Pity it is only for Windows. -- Robert Berman The Torah says, Love your neighbor as yourself. The Buddha says, There is no self. So, maybe, we're off the hook. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pyhton editor
Hi, On Feb 17, 2012, at 20:23, Robert Berman berman...@gmail.com wrote: On Fri, Feb 17, 2012 at 10:48 PM, Kapil Shukla shukla.ka...@gmail.com wrote: All Couple of weeks ago I was looking for a nice free IDE for python and got many wonderful suggestion form very helpful people. However I stumbled upon PyScripter and I find it really amazing. I feel once u try it you will hook on to it for ever Thanks Best Regards Kapil Pity it is only for Windows. I can highly recommend Sublime Text 2. Just recently switched to it from Eclipse. lzantal -- Robert Berman The Torah says, Love your neighbor as yourself. The Buddha says, There is no self. So, maybe, we're off the hook. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor