Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exploration of supporting single-dispatched functions for argname() #53

Closed
pwwang opened this issue Jul 6, 2021 · 2 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@pwwang
Copy link
Owner

pwwang commented Jul 6, 2021

>>> from varname import argname
>>> from functools import singledispatch
>>> 
>>> @singledispatch
... def add(a, b):
...     ...
... 
>>> @add.register(int)
... def _(a, b):
...     aname = argname(a, b)
...     print(aname)
...     return a + b 
... 
>>> def mul(a, b):
...     aname = argname(a, b)
...     print(aname)
...     return a * b
... 
>>> x = y = 1
>>> 
>>> mul(x, y)
('x', 'y')
1
>>> 
>>> add(x, y)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    add(x, y)
  File "/.../lib/python3.7/functools.py", line 840, in wrappe
r
    return dispatch(args[0].__class__)(*args, **kw)
  File "<input>", line 3, in _
    aname = argname(a, b)
  File "/.../varname/core.py", line 401, in argnam
e
    f"No value passed for argument {argnode.id!r}, "
ValueError: No value passed for argument 'a', or it is not an argument at all.
@pwwang pwwang added the enhancement New feature or request label Jul 6, 2021
@pwwang pwwang self-assigned this Jul 6, 2021
@pwwang
Copy link
Owner Author

pwwang commented Jul 6, 2021

@2d94eff

>>> from varname import argname
>>> from functools import singledispatch
>>> 
>>> @singledispatch
... def add(a, b):
...     ...
... 
>>> @add.register(int)
... def add_int(a, b):
...     aname = argname(a, b, func=add_int)
...     print(aname)
...     return a + b 
... 
>>> @add.register(str)
... def add_str(a, b):
...     aname = argname(a, b, dispatch=str) # specify a type
...     print(aname)
...     return a + b 
... 
>>> x = y = 1
>>> p = q = 'a'
>>> 
>>> add(x, y)
('x', 'y')
2
>>> add(p, q)
('p', 'q')
'aa'

@pwwang
Copy link
Owner Author

pwwang commented Jul 6, 2021

Close via v0.7.0

@pwwang pwwang closed this as completed Jul 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant