Globals or objects?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Feb 23 16:44:35 EST 2008


On Sat, 23 Feb 2008 08:16:44 -0500, Steve Holden wrote:

> Steven D'Aprano wrote:
>> On Fri, 22 Feb 2008 18:53:54 +0000, tinnews wrote:
>> 
>>>>> 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?
>> 
>> That's a *good* thing, not a problem. The whole idea is to get away
>> from the bad behaviour of globals, not find some other way to implement
>> it.
>> 
> I think that advocation of a global singleton is not really solving the
> problem. Not the problem I perceive, anyway. From your comments it's
> difficult to see whether we share the same perceptions, so let me
> elucidate.

Thanks for the details. I do agree with you. It wasn't me that suggested 
that the OP create a global singleton, or that using such a beast was a 
good plan. I hope that my response that [paraphrasing] a global singleton 
was good because it lets the developer STOP using a global singleton can 
be seen in context of tinnews badgering me again and again to explain why 
using a global singleton is better than an old-style global variable. 
That's not a position I would take.

[...]
> After all, some things *have* to be global for our programs to make any
> sense at all, unless you want to adopt a truly functional style that has
> never appealed to me. I like my programs to have state.

Some things are global, yes, but very few of them need to be "global 
variables" in the sense we're discussing. Actually, in the sense of the 
OP's example, I don't believe anything *needs* to be a global variable, 
unless it's a deliberate design choice (e.g. a global flag controlling 
some aspect of program behaviour, which I see as a legitimate if 
restrictive choice).


-- 
Steven



More information about the Python-list mailing list