[Python-ideas] Re: Extract variable name from itself
Good ideas, however not robust: a = 1 b = 1 print(id(a))# 4536318072 print(id(b))# 4536318072 > On 14 Sep 2023, at 16:35, Jonathan Fine wrote: > > POSTSCRIPT > > We can also use locals() to 'inverse search' to get the name, much as in the > original post. > > >>> locals()['x'] > (0, 1, 2, 3) > >>> id(x) > 139910226553296 > >>> id(locals()['x']) > 139910226553296 > -- > Jonathan > ___ > 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/MPPLJ6MXZDNA7J75T7H76LNYZGHCEG37/ > Code of Conduct: http://python.org/psf/codeofconduct/ ___ 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/7BXP63XG4PSQ2YFLCRXUEXJNNIQYEB4H/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Extract variable name from itself
Jonathan Fine writes: > We can also use locals() to 'inverse search' to get the name, much > as in the original post. As has already been explained, locals() (and any namespace for that matter) is a many-one mapping, and therefore the inverse is not well-defined. At least for the 'print(f"count is {count}")' example, you really need the compiler's help to get it right, unless you are willing to do it the other way around: def debug_name(name: str) -> None: print(f"{name} is {eval(name)}") but in general that's fraught with all the problems of using eval(). Steve ___ 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/TIDEI74KQMEUCORSB7XMTBITDFYSK7D7/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Extract variable name from itself
POSTSCRIPT We can also use locals() to 'inverse search' to get the name, much as in the original post. >>> locals()['x'] (0, 1, 2, 3) >>> id(x) 139910226553296 >>> id(locals()['x']) 139910226553296 -- Jonathan ___ 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/MPPLJ6MXZDNA7J75T7H76LNYZGHCEG37/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Extract variable name from itself
Perhaps use the id of an object instead of its value. Here's how it might work >>> store = {} >>> def wibble(fn): ... val = fn() ... name = fn.__name__ ... store[id(val)] = name ... return val >>> @wibble ... def ddd(): ... return tuple(range(4)) >>> ddd (0, 1, 2, 3) >>> store[id(ddd)] 'ddd' >>> >>> x = ddd >>> ddd = None >>> store[id(x)] 'ddd' I hope this helps. -- Jonathan ___ 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/V2RON2KVFVECTZMLXHJV3KJKNSLNQHA5/ Code of Conduct: http://python.org/psf/codeofconduct/