Yesterday I had the opportunity to speak with Anders Hejlsberg, father of both Turbo Pascal and C#. Of course I had to scratch my dynamic language itch, so we talked some about that. The upshot is that Anders believes compile-time type checking is valuable, but also thinks we can (and probably should) use type inferencing to make static type checking feel more dynamic.
During our conversation, he reminded me of an issue that I've been meaning to ask the Python folks to comment on. To illustrate it, consider exhibits A, B, and C.
Exhibit A. This Python program produces no compile-time error when the misspelled variable aNyme is referenced. It produces an error at runtime.
$ cat loose.py aName = 'abc'; print '[' + aNyme + ']'; $ $ python -c "compile(open('loose.py').read(),'loose.py','exec')" $ $ python loose.py Traceback (most recent call last): File "loose.py", line 2, in ? print '[' + aNyme + ']'; NameError: name 'aNyme' is not defined
Exhibit B. This Perl program produces no compile-time or run-time error.
$ cat loose.pl my $aName = 'abc'; print "[" . $aNyme . "]\n"; $ $ perl -c loose.pl loose.pl syntax OK $ $ perl loose.pl $ []
Exhibit C. This Perl program produces a compile-time error.
$ cat strict.pl use strict; my $aName = 'abc'; print "[" . $aNyme . "]\n"; $ $ perl -c strict.pl Global symbol "$aNyme" requires explicit package name at strict.pl line 3. strict.pl had compilation errors. $ $ perl strict.pl Global symbol "$aNyme" requires explicit package name at strict.pl line 3. Execution of strict.pl aborted due to compilation errors.
A few others out there have made this observation, for example:
I find python confusing on the other hand. e.g. sysmsg = sysmsg.replace('&', ' ')In my use of Perl, I've sometimes had to relax the constraints imposed by "use strict" -- for example, with "no strict vars" when I'm dynamically conjuring variable names. But on the whole, I never felt (though I'm sure some do) that "use strict" seriously compromised Perl's essential dynamism.
what if you wrote "sysmgs = sysmsg.replace('&',' ')"
there is a small typo! In perl "use strict;" would find that for you, but python has no equivalent yet. [anonymous comment at LinuxJournal.com]
Are there reasons why Python can't, or shouldn't, support something like "use strict"?
Former URL: http://weblog.infoworld.com/udell/2004/03/05.html#a935