[Python-ideas] Re: Extract variable name from itself

2023-09-14 Thread Jonathan Fine
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)

[Python-ideas] Re: Extract variable name from itself

2023-09-14 Thread Jonathan Fine
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-id

[Python-ideas] Re: Extract variable name from itself

2023-09-14 Thread Stephen J. Turnbull
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"coun

[Python-ideas] Re: Extract variable name from itself

2023-09-14 Thread Dom Grigonis
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