[sclug] Python global variable question

Andrew Nelis andrew.nelis at gmail.com
Thu Nov 26 10:50:57 UTC 2009


2009/11/26 pieter claassen <pieter at claassen.co.uk>:
> I am trying *not* to store a counter in a global variable in Python. Does
> anybody have a suggestion for how to do this elegantly?

There might be some way within your source to do it more elegantly but
I think the general solution is to hide it within a pair of functions;

__the_counter = 0

def getCounter():
    return __the_counter

def incrementCounter( amount=1 ):
    global __the_counter
    __the_counter += amount
    return __the_counter

Then use the functions rather than the variable.

Cheers,

Andy.



More information about the Sclug mailing list