> On 22 Jan 2020, at 14:39, Alex Hall <alex.moj...@gmail.com> wrote:
> 
> 
> 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

That's pretty cool!
_______________________________________________
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/CDE44AP7P3ANO656EUQ4DOGQSXNWMWG3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to