"no variable or argument declarations are necessary."

Duncan Booth duncan.booth at invalid.invalid
Wed Oct 5 03:49:36 EDT 2005


Paul Rubin wrote:

> Brian Quinlan <brian at sweetapp.com> writes:
>> Have those of you who think that the lack of required declarations in
>> Python is a huge weakness given any thought to the impact that adding
>> them would have on the rest of the language? I can't imagine how any
>> language with required declarations could even remotely resemble
>> Python.
> 
> What's the big deal?  Perl has an option for flagging undeclared
> variables with warnings ("perl -w") or errors ("use strict") and Perl
> docs I've seen advise using at least "perl -w" routinely.  Those
> didn't have much impact.  Python already has a "global" declaration;
> how does it de-Pythonize the language if there's also a "local"
> declaration and an option to flag any variable that's not declared as
> one or the other?

The difference is that perl actually needs 'use strict' to be useful for 
anything more than trivial scripts. Without 'use strict' you can reference 
any variable name without getting an error. Python takes a stricter 
approach to begin with by throwing an exception if you reference an 
undefined variable.

This only leaves the 'assigning to a different name than the one we 
intended' problem which seems to worry some people here, and as has been 
explained in great detail it incurs a cost to anyone reading the code for 
what most Python users consider to be a very small benefit.

If you think variable declarations should be required, then you presumably 
want that to cover class attributes as well as local and global 
variables. After all assigning to 'x.i' when you meant 'x.j' is at least as 
bad as assigning to 'i' instead of 'j'. But unless you know the type of 
'x', how do you know whether it has attributes 'i' or 'j'? So do we need 
type declarations, or perhaps we need a different syntax for 'create a new 
attribute' vs 'update an existing attribute', both of which would throw an 
exception if used in the wrong situation, and would therefore require lots 
of hasattr calls for the cases where we don't care.




More information about the Python-list mailing list