"no variable or argument declarations are necessary."

Mike Meyer mwm at mired.org
Thu Oct 6 22:18:03 CEST 2005

Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:

> Mike Meyer <mwm at mired.org> writes:
>> I think we're using different definitions of statically typed
>> here. A language that is statically typed doesn't *need* type
>> inferencing - the types are all declared! Type determines the thypes
>> by inferenceing them from an examination of the program. 
> I thought static typing simply means the compiler knows the types of
> all the expressions (whether through declarations or inference) so it
> can do type checking at compile time:
>> So, for instance, it can determine that this function:
>> def foo():
>>     return 1
>> Won't ever return anything but an integer.
> Static typing in this case would mean that re.match('a.*b$', foo())
> would get a compile time error, not a runtime error, since re.match
> expects two string arguments.  This can happen through type inference
> w/o declarations.

Except for two problems:

One you noted:

> Note apropos the private variable discussion that CPython can't
> guarantee that foo() always returns an integer.  Something might
> change foo.func_code.co_code or something like that.

Two is that dynamic binding means that foo may not refer to the above
function when you get there at run time.

>> Maybe you're still writing code for a language with declerations? I
>> never felt that need. Then again, I came to Python from a language
>> that didn't require declerations: Scheme.
> I've done a fair amount of Lisp programming and have found the lack of
> compile-time type checking to cause about the same nuisance as in
> Python.

So have I - basically none at all.

>> > Well, in the end, I would really like an *option* at the beginning of a
>> > module file requiring variable declaration for the module. It would
>> > satisfy both the ones who want and the ones who don't want that ...
>> Nope. It would just change the argument from "Python should have ..."
>> to "You should always use ..." or "Module foo should use ...".
> Perl has a feature like that right now, and it doesn't lead to many such
> arguments.

As noted elsewhere, Perl isn't a good comparison. You don't simply say
"This variable exists", you say "this variable is local to this
function". Undeclared variables are dynamically bound, which means you
can get lots of non-obvious, nasty bugs that won't be caught by unit
testing. Making all your variables lexically bound (unless you really
need a dynamically bound variable) is a good idea. But that's already
true in Python.

Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list