On Thu, Aug 22, 2013 at 3:04 PM, Alan Gauld <alan.ga...@btinternet.com> wrote: > On 22/08/13 13:36, Matthew Ngaha wrote:
> Global variables in themselves are not the problem. > It's how they tend to get used that causes problems. > Globals that are only changed via a set of > dedicated functions are not topo much of a > problem - although they should probably be > bundled as a class (or module) > > Usually that should be in a class. A class represents > a set of operations using common data. Therefore shared > state would naturally fit in a class along with the > operations which depend on/modify that state. > Oh this makes a lot of sense. Sorry about disagreeing with Chris i now see that perhaps a class is definately the best choice for keeping a state variable. > Thats more problematic and usually a sign of a bad design. > Even if using global variables you should modify them > explicitly via return values of functions rather than > as hidden side-effects inside other functions. > > mystate = changestate(mystate, some, other, args) Yeah i have to agree about the bad design now i think about it. I didn't see the issues before reading this > It is possible but only by playing silly games with > semantics such as: > > class MyProgram > global1 = 0 > global2 = True > > [Or alternatively you can hide them inside a database.] > > Now technically there are no globals but in fact we are > just moving them inside the meaningless class and have > all the same potential issues with global side effects > etc. About the class variables, i have used them a lot without realising they had all the same side effects as global variables. Something i now have to try a different approach with. > In my experience there are usually a few globals required > for any meaningful program. It's not avoiding globals > completely that's important, it's being careful to use > them sensibly and with good adherence to the principles > of coupling and cohesion in the design. Thanks alot! This is definately a lot of food for choice and will help in my future decisions. I'm happy i asked this question as i can honestly say i had been developing some bad habbits. The responses have definately been helpful about program design and not just global variables. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor