Class Variable Question

Robert Amesz rcameszREMOVETHIS at dds.removethistoo.nl
Mon Apr 16 22:57:05 EDT 2001


Steve Holden wrote:

>"Douglas Alan" <nessus at mit.edu> wrote in message
>news:lc4rvpih6y.fsf at gaffa.mit.edu...
>
>> [Snip]
>>
>> A language should be as resistant to typos as it is convenient to
>> make it.  When the programmer makes a typo, it should in most
>> cases cause the program to generate an obvious error.  To disagree
>> with this premise is to agree with the Perl philosophy, rather
>> than the Python philosophy. 
>>
>> Thank goodness Python wasn't designed with the philosophy that 
>> unit tests should be used to catch all typos.  If it were, I'd 
>> hate Python as much as I hate Perl. 
>
> [Snip]
>
> I am a little concerned that if "option explicit" or its equivalent
> became available then many novices would try to use it and then not
> understand why code which uses __setattr__() to create name
> bindings on the fly ceased to work. 

Well, there's no reason why you can't have declared variables on a 
function by function basis. Something like:

def func(x):
    local y
    y = 2 * x
    return y

The 'local' keyword then would be the counterpart of 'global'. Whenever 
the Python compiler would encounter 'local' it would signal that _for 
that function only_ all variables should be declared before use. A 
feature like that shouldn't be difficult to add, and wouldn't break any 
existing code.

Of course, that doesn't help you when you mistype the name of a class 
member, but that's a different problem, which - given the the nature of 
class relationships - is impossible to solve if the bytecompiler only 
examines the context of a single class. Instead, those checks must be 
made during runtime, and so the behaviour of the bytecode interpreter 
should be changed, and that should be done with great care.

Perhaps problems like this should be left to a tool like PyChecker, 
which can examine multiple related classes simultaneously.


Robert Amesz



More information about the Python-list mailing list