[Python-ideas] PEP 484 (Type Hints) -- first draft round
flying-sheep at web.de
Sat Jan 17 12:41:46 CET 2015
Guido van Rossum <guido at python.org> schrieb am Fri Jan 16 2015 at 22:31:11:
> i don’t think comments should ever have an impact on the code.
> They don't. They also change the errors emitted by the static checker
> (which is not invoked when you run the code -- it is like a linter).
still, comments are in my eyes purely documentational. the only exception
in python i can think of, doctest, only exists to ensure that no examples
you give start breaking, i.e. doctests are ran to keep the *comments* up to
date, not the code.
and annotations are a syntax-level element. using comments forces the
linter to use an alternative AST with comments preserved, not the AST
CPython gives you for free.
and i’m looking into the future: python interpreters/VMs could choose to
optimize e.g. List[double] based on type annotations, instead of just
linting. in which case comments would become part of the code.
> i’d prefer “assert isinstance(...)” for type annotations on variables.
> But that *would* affect runtime, by slowing down execution. Also for
> generic types the type checking could be either very slow (if checking
> List[int] would check every item of a list) or always fail (if a type
> variable is involved).
> See discussion at https://github.com/ambv/typehinting/issues/35
optimized package builds strip out assertions, no? and if course
isinstance(, List[int]) shouldn’t fail. that would be very surprising!
i’ll check out the issue.
> But there is a lot of code that really needs the concrete types. E.g.
> list+list returns a list, but sequence+sequence may fail (as list+tuple
> exemplifies). Or list.sort. Etc.
i commented in the issue: we could specify that behavior in new ABCs or
even add it as mixin methods. nothing prevents MutableSequence, which has
.reverse, from having .sort, as well.
will we be able to use Union[...] in places like an except clause, where
>> tuples are used to mean the same right now? would make sense imho
> No. Remember TOOWTDI. (Also the PEP intentionally stays far away from
> exception handling.)
yeah, i just though that isinstance(foo, (str, bytes)) basically means “is
foo an instance of either str or bytes”, or in other words “is foo an
instance of the union type str|bytes”.
the one way to do it would have been Union[str, bytes] if python had Union
from the beginning, i’m sure!
what are ItemsView and KeysView for? what makes them different from
>> FrozenSet or Sequence?
> They exist in collections.abc. Note that KeysView and ItemsView are
> (abstract) Sets, but ValuesView is not (you can have multiple equal values).
ah, get it! they provide mixins powered by suppying a to-be-wrapped Mapping
instance to MappingView’s __init__!
this should be documented, else nobody knows *how* they mix in their
> --Guido van Rossum (python.org/~guido)
thanks for the explanations, and as always: thanks for this most beautiful
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas