You can achieve this now with my executing library:
https://github.com/alexmojaki/executing

```
import ast
import inspect

import executing


def nameof(_):
    frame = inspect.currentframe().f_back
    call = executing.Source.executing(frame).node
    arg = call.args[0]
    if isinstance(arg, ast.Name):
        return arg.id
    elif isinstance(arg, ast.Attribute):
        return arg.attr
    else:
        raise SyntaxError


foo = 3
print(nameof(foo))

print(f"{nameof(ast.Add)} = {ast.Add}")
```

Other tricks of this nature can be found here:
https://github.com/alexmojaki/sorcery
_______________________________________________
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/XSTWIXDFPSMMIGSXVPKEEGTMFDQ66JUM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to