Михаил Крупенков:
> Yes, I want that when Void is received in a function parameter it is not
processed:
>
> func(Void) == func() == "default"

Mathew Elman:
> I believe this is a rebirth of a request that has come up many times
before, which is to have something like javascript's `undefined` where it
means "use the default value" if passed to a function that has a default
value or "value not provided" (slightly different to "None").

I think this can be achieved with a decorator.
>>> import inspect, functools
>>>
>>> class Void:
...     pass
...
>>> def handle_defaults(f, undefined=Void):
...     sig = inspect.signature(f)
...     @functools.wraps(f)
...     def defaults_handled(*args, **kwargs):
...         bound = sig.bind(*args, **kwargs)
...         bound.apply_defaults()
...         for name, value in list(bound.arguments.items()):
...             if value is undefined:
...                 field = sig.parameters[name]
...                 if field.default is not inspect._empty:
...                     bound.arguments[name] = field.default
...         return f(*bound.args, **bound.kwargs)
...     return defaults_handled
...
>>>
>>> @handle_defaults
... def func(val="default"):
...     return val
...
>>> func(Void)
'default'
>>>
>>> def wrapper(val=Void):
...     return func(val)
...
>>>
>>> wrapper()
'default'

(In case formatting is screwed up in the above example, see
https://gist.github.com/lucaswiman/995f18763d088555547a74864254ac0b)

I would recommend using Ellipsis (...) rather than Void for this purpose so
you have a convenient syntax.

Best wishes,
Lucas
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WCLWTRKOAYHYNVMEHBOHLPEX523CSFGC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to