18.05.2015, 02:50, Guido van Rossum kirjoitti:
On Sun, May 17, 2015 at 3:07 PM, Alex Grönholm
<alex.gronh...@nextday.fi <mailto:alex.gronh...@nextday.fi>> wrote:
Looking at PEP 484, I came up with two use cases that I felt were
not catered for:
1. Specifying that a parameter should be a subclass of another
(example: Type[dict] would match dict or OrderedDict; plain
"Type" would equal "type" from builtins)
I don't understand. What is "Type"? Can you work this out in a full
example? This code is already okay:
def foo(a: dict):
...
foo(OrderedDict())
This code is passing an /instance/ of OrderedDict. But how can I specify
that foo() accepts a /subclass/ of dict, and not an instance thereof?
A full example:
def foo(a: Type[dict]):
...
foo(dict) # ok
foo(OrderedDict) # ok
foo({'x': 1}) # error
1. Specifying that a callable should take at least the specified
arguments but would not be limited to them: Callable[[str,
int, ...], Any]
Case #2 works already (Callable[[str, int], Any] if the
unspecified arguments are optional, but not if they're mandatory.
Any thoughts?
For #2 we explicitly debated this and found that there aren't use
cases known that are strong enough to need additional flexibility in
the args of a callable. (How is the code calling the callable going to
know what arguments are safe to pass?) If there really is a need we
can address in a future revision.
Consider a framework where a request handler always takes a Request
object as its first argument, but the rest of the arguments could be
anything. If you want to only allow registration of such callables, you
could do this:
def calculate_sum(request: Request, *values):
return sum(values)
def register_request_handler(handler: Callable[[Request, ...], Any]):
...
--
--Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com