Global variables for python applications
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon May 17 20:22:52 EDT 2010
On Mon, 17 May 2010 23:54:38 +0100, Rhodri James wrote:
> On Mon, 17 May 2010 05:29:20 +0100, Steven D'Aprano
> <steven at remove.this.cybersource.com.au> wrote:
>
>> On Sun, 16 May 2010 18:57:15 -0700, John Nagle wrote:
>>
>>> James Mills wrote:
>>>> The only place global variables are considered somewhat "acceptable"
>>>> are as constants in a module shared as a static value.
>>>
>>> Python really ought to have named constants.
>>
>> +1
>>
>> Unfortunately, it will most likely require new syntax, and semantics.
>> While the concept of a constant is pretty straightforward for immutable
>> types like ints and strings, what about mutable types?
>>
>> And then there's the moratorium, so even if we had agreement on
>> semantics and syntax, and a patch, it couldn't be deployed for a few
>> years.
>
> Careful, you're reconflating two concepts that John separated:
> mutability of an object and binding of a name.
In my own head the two issues of mutability and rebinding were completely
separate, but I see now that didn't come through as clearly as I hoped in
my post. My apologies for any confusion.
> I'm on the side of
> 'named constant' meaning 'this name (in this scope) is bound to this
> object and cannot be rebound.' That would cover most of the cases
> people care about, and the gotchas are essentially the same as with
> default arguments.
I think it is an abuse of the term constant to allow you to talk about a
mutable object being "constant", since it can vary. Generally, you don't
care about identity, only equality. Making up a syntax on the spot:
constant pi = [3.1415]
assert pi = 3.1415
pi[0] = 3
assert pi = 3.1415
makes a mockery of the concept of a constant.
Having said that, recognising mutable objects is a hard problem, so
simply for reasons of practicality we might have to just live with the
limitation that "constants" can be mutated if the object supports it.
> But yes, it would require new syntax.
Ideally, but there may be alternatives. I've already mentioned the
Cookbook recipe, or perhaps something like:
import constants
constants.register('pi')
pi = 3.1415
--
Steven
More information about the Python-list
mailing list