Re: N00b question on Py modules
On May 9, 2:47 pm, Sion Arrowsmith <[EMAIL PROTECTED]> wrote: ... > How so? Python style gurus discourage use of global variables. So > does all the C++ (and to a lesser extent C) advice I've ever > encountered. And Java outright forbids the concept. Class variables (public static), are the equivalent of global variables in Java, and can be an equal pain. Singleton objects can cause similar problems, since they are essentially global objects, and changing the values of any of their members can cause wierd behaviour in otherwise unrelated parts of the code. So Java isn't by any means immune to the global variable problem, it just has different names for them! -- http://mail.python.org/mailman/listinfo/python-list
Re: N00b question on Py modules
<[EMAIL PROTECTED]> wrote: >Thanks a lot for the responses ppl. Python's treatment of global >variables was an eye-opener. I have coded in Java & C/C++ in the past >and there the behaviour is diametrically opposite. How so? Python style gurus discourage use of global variables. So does all the C++ (and to a lesser extent C) advice I've ever encountered. And Java outright forbids the concept. It's one area where there seems to be universal agreement. -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ "Frankly I have no feelings towards penguins one way or the other" -- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
Re: N00b question on Py modules
Thanks a lot for the responses ppl. Python's treatment of global variables was an eye-opener. I have coded in Java & C/C++ in the past and there the behaviour is diametrically opposite. Cheers -- http://mail.python.org/mailman/listinfo/python-list
Re: N00b question on Py modules
On Mon, 07 May 2007 00:00:38 -0700, lokesh.jagasia wrote: > I expected to see an output of 0 followed by 1. But it turns out that > the _exitcode variable is not changed at all. It seems that > setExitCode() might be editing a local copy of the _exitcode variable. Yes, that's exactly what is happening. > But then, how do I tell it to change the value of the module variable > and not its local variable. (1) Don't do that. (2) If you think you really need to do it, you don't. (3) If you REALLY need to do it, use the statement: global in your function. Over-use of global variables is one of the sins of programming. Some people might even say that using ANY global variables is a sin. I'm not that strict, but I encourage you to avoid global variables if you can. See here for more details: http://en.wikipedia.org/wiki/Global_variable -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: N00b question on Py modules
[EMAIL PROTECTED] wrote: > Hi. Sorry to sound like a noob but that's what I am when it comes to > Python. I just wrote the below module and it behaves funny. > > My python module: > > _exitcode = 0 > > def setExitCode(): > _exitcode = 1 > > if __name__ == '__main__': > print _exitcode > setExitCode() > print _exitcode > > Actual O/P: > 0 > 0 > > I expected to see an output of 0 followed by 1. But it turns out that > the _exitcode variable is not changed at all. It seems that > setExitCode() might be editing a local copy of the _exitcode variable. > But then, how do I tell it to change the value of the module variable > and not its local variable. > > I've been through the modules section of Python docs and a few ebooks > as well, all suggest that it shouldn't be working this way. > > Please help out ppl. > It's a scoping problem. The line _exitcode = 0 creates a (module level) global object. But in def setExitCode(): _exitcode = 1 you are running into Python's default presumption that variables assigned to in a function are *local* to that function. And like all local variables, they can be set and used within the function, but are independent of objects outside the function. If you want to assign to a global object from within a function, then you must explicitly say so: def setExitCode(): global _exitcode _exitcode = 1 See: http://docs.python.org/ref/global.html Gary Herron > Thanks > > -- http://mail.python.org/mailman/listinfo/python-list
Re: N00b question on Py modules
In <[EMAIL PROTECTED]>, lokesh.jagasia wrote: > My python module: > > _exitcode = 0 > > def setExitCode(): > _exitcode = 1 > > if __name__ == '__main__': > print _exitcode > setExitCode() > print _exitcode > > Actual O/P: > 0 > 0 > > I expected to see an output of 0 followed by 1. But it turns out that > the _exitcode variable is not changed at all. It seems that > setExitCode() might be editing a local copy of the _exitcode variable. > But then, how do I tell it to change the value of the module variable > and not its local variable. Any name that gets bound to an object within a function is local to that function unless you declare it as ``global``. But using lots of global variables is considered bad style so you may think about rewriting functions with ``global`` names to return the value(s) instead: _exitcode = 0 def set_exitcode(): return 1 if __name__ == '__main__': print _exitcode _exitcode = set_exitcode() print _exitcode Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: N00b question on Py modules
Hi this variable is local to function do this def setExitCode(): global _exitcode _exitcode = 1 On 7 May 2007 00:00:38 -0700, [EMAIL PROTECTED] < [EMAIL PROTECTED]> wrote: Hi. Sorry to sound like a noob but that's what I am when it comes to Python. I just wrote the below module and it behaves funny. My python module: _exitcode = 0 def setExitCode(): _exitcode = 1 this variable is local to function if __name__ == '__main__': print _exitcode setExitCode() print _exitcode Actual O/P: 0 0 I expected to see an output of 0 followed by 1. But it turns out that the _exitcode variable is not changed at all. It seems that setExitCode() might be editing a local copy of the _exitcode variable. But then, how do I tell it to change the value of the module variable and not its local variable. I've been through the modules section of Python docs and a few ebooks as well, all suggest that it shouldn't be working this way. Please help out ppl. Thanks -- http://mail.python.org/mailman/listinfo/python-list -- Regards-- Rishi Pathak National PARAM Supercomputing Facility Center for Development of Advanced Computing(C-DAC) Pune University Campus,Ganesh Khind Road Pune-Maharastra -- http://mail.python.org/mailman/listinfo/python-list
Re: N00b question on Py modules
On Mon, May 07, 2007 at 12:00:38AM -0700, [EMAIL PROTECTED] wrote: > Hi. Sorry to sound like a noob but that's what I am when it comes to > Python. I just wrote the below module and it behaves funny. > > My python module: > > _exitcode = 0 > > def setExitCode(): > _exitcode = 1 > > if __name__ == '__main__': > print _exitcode > setExitCode() > print _exitcode > > Actual O/P: > 0 > 0 > > I expected to see an output of 0 followed by 1. But it turns out that > the _exitcode variable is not changed at all. It seems that > setExitCode() might be editing a local copy of the _exitcode variable. > But then, how do I tell it to change the value of the module variable > and not its local variable. _exitcode is a global variable in your program. In functions (def) you can read global variables. But if you change or reassign them the change will be only local to the function. If you want to change the global variable your def needs to be: def setExitCode(): global _exitcode _exitcode = 1 Kindly Christoph -- http://mail.python.org/mailman/listinfo/python-list
N00b question on Py modules
Hi. Sorry to sound like a noob but that's what I am when it comes to Python. I just wrote the below module and it behaves funny. My python module: _exitcode = 0 def setExitCode(): _exitcode = 1 if __name__ == '__main__': print _exitcode setExitCode() print _exitcode Actual O/P: 0 0 I expected to see an output of 0 followed by 1. But it turns out that the _exitcode variable is not changed at all. It seems that setExitCode() might be editing a local copy of the _exitcode variable. But then, how do I tell it to change the value of the module variable and not its local variable. I've been through the modules section of Python docs and a few ebooks as well, all suggest that it shouldn't be working this way. Please help out ppl. Thanks -- http://mail.python.org/mailman/listinfo/python-list