Bad programming style?

Alex Martelli Alex.Martelli at think3.com
Fri Dec 24 02:48:44 EST 1999


Carel writes:

> Why not use the following:
> 
> >>> locals()['r'] = 'ff'
> >>> print r
> ff
> 
Uh, perhaps because the docs specifically tell you not to...?-)

"WARNING: the contents of this dictionary should not be
modified; changes may not affect the values of local
variables used by the interpreter", is the way the Library
Reference puts it; I don't see how it could be clearer.

> I think this isn't garanteed to work in future versions of Python, but
> it sure is crisp to me. And no hidden explosives involved at all:)
> 
I think the quote from the docs tell me this is anything BUT
"guaranteed" to work in THIS version, either -- it may well
depend on the port, the phase of the moon, whatever; and
if it fals it will probably fail silently, undetected, changing
program semantics in hard-to-predict ways.

I think this "feature" makes a good case for something I
was wishing for since very early on in my (not yet very
long:-) Python involvement -- a way to "lock" a mapping
so that attempts to write to it will fail *non*-silently, i.e.,
raise exceptions.  If such a 'locked' state was available,
I imagine locals() would make use of it, preventing such
erroneous usage.

(Yeah, yeah, I know -- in the Perl community, people call
this the "bondage & discipline" approach to programming,
mostly with derisive intent; but I still think it's worthwhile to
actively try to prevent erroneous usage when feasible -- I
guess this comes from being a BDSM enthusiast?-)


Alex





More information about the Python-list mailing list