Re: addition of "nameof" operator

Jan. 22, 2020
1:17 p.m.
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
1883
Age (days ago)
1883
Last active (days ago)
1 comments
2 participants
participants (2)
-
Alex Hall
-
Anders Hovmöller