variable declaration

Nick Coghlan ncoghlan at iinet.net.au
Sat Feb 5 11:45:33 EST 2005


Alex Martelli wrote:
> 'global' is an ugly wart, to all intents and purposes working "as if" it
> was a declaration.  If I had to vote about the one worst formal defect
> of Python, it would surely be 'global'.
> 
> Fortunately, it's reasonably easy to avoid the ugliness, by avoiding
> rebinding (within functions) global variables, which tend to be easy. 

Hear, hear! And if I could write "gbls = namespace(globals())" in order to deal 
with those rare cases where I *do* need to rebind globals, all uses of the 
keyword could be handled nicely, while entirely eliminating the need for the 
keyword itself.

> *ONLY* that tiny subset of such typos which happened on the left of a
> plain '=' (since all others, happening on the RIGHT of an '=' or on the
> left of an _augmented_ '=', were already caught), and ONLY regarding
> barenames (such typos on any but the rightmost component of compound
> names were already caught intrinsically, and catching those on the
> rightmost component is trivially easier than introducing a {YECCCCHH}
> 'vars' as you so stubbornly insist)...

Would you be as violently opposed to a 'rebinding' augmented assignment operator?

Since bare assignment statements essentially serve the purpose of variable 
declarations, I sometimes *would* like a way to say 'bind this existing name to 
something different'. A rebinding operation would provide a way to make that 
intention explicit, without cluttering the language with useless declarations.

In addition to detecting typos in local variable names, it would *also* address 
the problem of detecting typos in the right-most name in a compound name (e.g. 
making a habit of always using the rebinding operator when modifying member 
variables outside of __init__ would make it easier to avoid inadvertently 
creating a new instance variable instead of modifying an existing one)

With a rebinding operator available, the only typos left to slip through the net 
are those which match an existing visible name and those where the programmer 
has explicitly requested an unconditional name binding by using '=' and then 
made a typo on the left hand side.

Cheers,
Nick.
Did I mention the possible incidental benefit of reducing the whinging about the 
lack of variable declarations?

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list