PEP 3107 and stronger typing (note: probably a newbie question)

Alex Martelli aleax at mac.com
Fri Jun 22 17:30:14 CEST 2007


Dave Baum <Dave.Baum at motorola.com> wrote:
   ...
> I still think it would be handy to easily specify the expected types of
> function arguments.  I sometimes write code in this pattern:
> 
> def foo(a, b):
>     "a, b - instances of Bar"
>     assert isinstance(a, Bar)
>     assert isinstance(b, Bar)
>     # do some stuff

so in 3.0 you'll be able to spell that

@checkanddoc
def foo(a: Bar, b: Bar): ...

for some suitable decorator checkanddoc (and be assured that there will
be a bazillion such decorators written to exploit the new syntax, of
varying intent and quality).  That's why 3.0 introduces that syntax
(args can be spelled <name>:<expr>) without giving it any semantics
beyond the fact that the info is recorded with the function object and
can be introspected from it -- enabling a thousand flowers to bloom in
terms of such decorators.

> On the downside, this sort of mechanism might do more harm than good.
> For one thing, it would really clash with duck typing.  For another, 
> anyone coming to Python from Java/C++ would probably use those 
> declarations *everywhere*, even when there isn't a good reason to limit
> the type.

Sure, even more than they currently use wanton isinstance calls (or even
worse, type(x)==... checks).  But hopefully the new "syntax hook" will
also allow GOOD decorators to emerge (e.g., ones doing adaptation rather
than mere checks).


Alex



More information about the Python-list mailing list