"no variable or argument declarations are necessary."

Roy Smith roy at panix.com
Fri Oct 7 06:01:00 EDT 2005


"Ben Sizer" <kylotan at gmail.com> wrote:
> It's started to get very misleading - Python gives you plenty of
> type-checking, as we all know, just not at compile-time.

There's more to it than just that.  Python's type checking is not just not 
done at compile time, it's done as late in run time as possible.  One might 
call it just-in-time type checking.

It's not hard to imagine a Python-like language which included (perhaps 
optional) variable declarations.  A declaration would essentially be an 
assertion which was checked after each assignment to that name.  So, you 
could write:

int i = 5
i = 5.6

and the second statement would throw TypeError.  This would give you 
C++/Java style type safety, but it still wouldn't be compile time.

Perhaps a better way to describe it is that the checking isn't an is-a 
assertion, but an acts-like assertion (sort of like Java's interfaces).  To 
take an example, in the function:

def first3(y):
    if len(y) < 3:
         return y
    return y[0:3]

all I really need from the argument is that I can call len() on it and it 
can be sliced.  An easy way to describe this would be to say that y must be 
a sequence, but that's not strictly accurate, since I can easily declare my 
own class which meets those requirements without being a subclass of 
sequence (even ignoring for the moment that 'sequence', while talked about 
in the documentation, doesn't actually exist as something you can subclass).



More information about the Python-list mailing list