force variable declaration?

Ian Parker parker at gol.com
Wed Oct 16 05:36:08 EDT 2002


In article <ao3nr5$358$1 at news1.wdf.sap-ag.de>, Peter Rams
<peter.rams at sap.com> writes
>Hi Jeff,
>
>thanks for your answer, I meant something like this:
>
>counter = 0
>while counter < 100:
>    conuter = counter + 1
>
>leads to an endless-loop because of typing error of counter. In other
>languages I have to declare all variables, it would look for example like
>this:
>
>int counter
>counter = 0
>while counter < 100:
>   conuter = counter + 1
>
>Then the syntax check says: no variable "conuter" declared. In Perl, for
>example, there is a statement (I forgot how it's like), where you can force
>the programmer to declare his variables. Is there something similar in
>python?
>
>Thanks,
>Peter
>
>
>"Jeff Davis" <jdavis at empires.org> wrote in message
>news:6Dbp9.69869$X9.12557473 at twister.socal.rr.com...
>>
>> what do you mean force variable declaration? you mean type checking?
>>
>> You can't really force python to limit you like that. That type of
>checking
>> is better served by a tool independent of the interpreter. For example,
>> pychecker is great for spotting probable errors in your code, even when
>> the interpreter doesn't complain. See if pychecker is able to help spot
>> your error.
>>
>> That way, we have the flexibility of python's dynamic typing, but we can
>> spot our errors quickly as well.
>>
>> Regards,
>>         Jeff
>>
>> > Hi,
>> >
>> > is it possible to force variable declaration in python (like it is for
>> > example in perl)? A few days ago I had an anoying error because of a
>> > typing error of a variable...
>> >
>> > Regards,
>> > Peter
>>
>
>

Peter

But the wonderful pychecker would also catch this, at least reporting
that " Local variable (conuter) not used".

Interestingly, pychecker didn't identify this in a file consisting only
of the code above, although it worked correctly with:

def co():
    counter = 0
    while counter < 100:
        conuter = counter + 1
        print counter

if __name__ == '__main__':
    co()

So that's probably a bug, or simply me.


-- 
Ian Parker



More information about the Python-list mailing list