Globals or objects?

tinnews at isbd.co.uk tinnews at isbd.co.uk
Fri Feb 22 13:53:54 EST 2008


Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:
> On Fri, 22 Feb 2008 12:01:20 +0000, tinnews wrote:
> 
> > Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:
> >> >> >.... but you do keep having to use a longer reference to the value
> >> >> >so what have you won?
> >> >> 
> >> >> Clarity, simplicity, robustness
> >> > 
> >> > Clarity - why is it clearer?
> >> 
> >> Consider two function calls:
> >> 
> >> 
> >> x = ham(arg, counter)
> >> y = spam(arg)
> >> 
> >> Both do exactly the same thing: ham() takes an explicit "counter"
> >> argument, while spam() uses a global variable. Which one makes it clear
> >> that it uses a counter, and which does not?
> >> 
> > But you're not comparing what the OP posted.  He was comparing a global
> > with an object with a single variable inside it.  Either would work with
> > the y = spam(arg) example above.
> 
> What do you mean by "an object with a single variable inside it"? I don't 
> understand what that is supposed to mean, or why you think it is the same 
> as a global. Do you mean a Singleton?
> 
> If so, then the answer is simple: using a Singleton argument instead of a 
> global is better, because with a global you are stuck to always using the 
> global (at least until you can re-write the code), but with the Singleton 
> argument, you may be enlightened and *not* use a Singleton.
> 
But if you stop using the Singleton the code no longer does the same
as it would with a global does it?

As I keep saying I agree wholeheartedly with the general idea that
globals are a bad thing.  However wrapping up what is effectively a
global in a different construct doesn't seem to me to be any help at
all.  What you need to do is take a long hard look at the global and
decide if there are better ways of doing it, not just simply wrap it
up in a class that really doesn't help at all.

-- 
Chris Green



More information about the Python-list mailing list