Importing variables non-deterministic?

Dave Angel davea at davea.name
Mon Aug 19 08:33:32 EDT 2013


Antoon Pardon wrote:

> Op 19-08-13 11:18, Chris Angelico schreef:
    <snip>
>> The issue
>> was regarding imports, and it's perfectly safe to import a constant,
>> even if the interpreter doesn't protect you from then being a total
>> idiot and changing it.
>
> Python doesn't have constants, so you statement about importing a
> constant doesn't make sense. The point is that python doesn't provide
> the mechanism for protecting names against reassignments. So you
> don't know whether the variable you think of as a constant is so
> in reality. And this from a pure language definition point of view.
> That you can use tools that make the interpreter no longer behave
> as the language should, doesn't negate that.
>

Who cares what the language "protects?"  I don't know any language
whose protections can't be at least partially bypassed by clever
foot-shooters.  In any case, we all know that Python doesn't protect
constants, so we're free to use the word in a friendlier way.

A Python constant is what I use as a constant.  I follow Pep-8 and make
it all caps.  So in any library I write struct_global.y  would be a bug
or a design flaw.  (And with a single character name like that, it
wouldn't be global in any case.  Single character names are reserved
for play code and for short loops)

And if I subsequently change it in my calling code, it isn't a
constant any more.  If I rebind the name, it's not even the same
variable any more.

That's a bug, not an exception to the rule "don't use global variables."

I also accept as a constant those values which are initialized
sufficiently early in the code that most places will only ever see the
final value.  Those may be global without worry.

-- 
DaveA




More information about the Python-list mailing list