force variable declaration?

Ian Parker parker at
Wed Oct 16 11:36:08 CEST 2002

In article <ao3nr5$358$1 at>, Peter Rams
<peter.rams at> 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
>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
>"Jeff Davis" <jdavis at> wrote in message
>news:6Dbp9.69869$X9.12557473 at
>> 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
>> 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


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__':

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

Ian Parker

More information about the Python-list mailing list