Globals or objects?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Feb 22 10:19:26 EST 2008


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.



-- 
Steven



More information about the Python-list mailing list