On 1/21/2020 4:32 PM, Andrew Barnert via Python-ideas wrote:
What would the semantics of nameof be in Python? Would it just be lambda obj:
obj.__name__? Or some fancy inspect-module style chain of “try this, then that,
then the other”? Or does it need to look at the compiled source code context or
something?
I think some wrapper around inspect.getmembers() would be the
easiest/best thing to do.
dict(inspect.getmembers(obj))['__name__']
With some error checking for the KeyError if inspect.getmembers can't
get a name for the object you passed in.
There are probably more performant ways to write this, but this was my
easy one-liner version.
Eric
_______________________________________________
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/AYZPTHSA65RWT2S5UOB7ERRTNSFX3SJU/
Code of Conduct: http://python.org/psf/codeofconduct/