[Tutor] global variables

Steven D'Aprano steve at pearwood.info
Thu Aug 22 18:00:20 CEST 2013


On 22/08/13 23:52, Chris Down wrote:
> On 2013-08-22 14:43, Matthew Ngaha wrote:
>> I don't feel my program needs a class. Also i have been told to stop
>> using classes by some very experienced Python programmers on irc even
>> though i don't see why. It's confusing being told different things.
>
> Well, if you want to store state, you should really be using a class.


Depends on how much state and what you do with it. But fundamentally, if you want to store state, you should try very, very hard to *avoid* storing state unless you really need to.

Or at least, make sure that there is a very strict demarcation between functions (or methods) which change that state, and those which do not. The ultimate aim is to avoid the bad parts of storing state, without necessarily going all the way to strict functional code:

- reduce or eliminate side-effects wherever possible;

- functions that have side-effects should do so only in well-understood and innocuous ways;

- eliminate coupling between unrelated parts of code;

- reduce coupling within related parts of code;

- avoid action at a distance whenever possible.


If you keep these aims in mind, the problem of global variables will solve itself.



-- 
Steven




More information about the Tutor mailing list