[Python-ideas] Extract variable name from itself

2023-09-12 Thread Dom Grigonis
As far as I understand, it is not possible. Is it? Something similar to: var = 710 variable_name = [k for k, v in locals().items() if v == 710][0] print("Your variable name is " + variable_name) ,except robust. Possible ways to expose it: * builtin function `varname(obj: object) —> str` * obje

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

2023-09-12 Thread Antoine Rozo
Why would an object always be linked to a variable name, and why only one name? Le mar. 12 sept. 2023 à 10:23, Dom Grigonis a écrit : > As far as I understand, it is not possible. Is it? > > Something similar to: > > var = 710 > variable_name = [k for k, v in locals().items() if v == 710][0] > p

[Python-ideas] Re: Have del return a value

2023-09-12 Thread Daniel Walker
Makes sense. On Tue, Sep 12, 2023 at 2:55 AM Rob Cliffe via Python-ideas < [email protected]> wrote: > > > On 08/09/2023 22:19, Christopher Barker wrote: > > On Fri, Sep 8, 2023 at 11:00 AM Barry Scott > wrote: > >> I see no need for del to return anything, you already have the reference >

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

2023-09-12 Thread Dom Grigonis
It wouldn’t. I know this is weird and not in line of how things work. This is more about simply getting reference variable name in locals() from the reference itself. But how would one access the variable name, when once called it returns the object it points to, not itself. So this would obviou

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

2023-09-12 Thread Chris Angelico
On Tue, 12 Sept 2023 at 19:51, Dom Grigonis wrote: > > It wouldn’t. I know this is weird and not in line of how things work. This is > more about simply getting reference variable name in locals() from the > reference itself. But how would one access the variable name, when once > called it ret

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

2023-09-12 Thread Dom Grigonis
Please read the next part of my e-mail too. It actually answer your question. > On 12 Sep 2023, at 13:00, Chris Angelico wrote: > > On Tue, 12 Sept 2023 at 19:51, Dom Grigonis wrote: >> >> It wouldn’t. I know this is weird and not in line of how things work. This >> is more about simply getti

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

2023-09-12 Thread Chris Angelico
On Tue, 12 Sept 2023 at 20:05, Dom Grigonis wrote: > > Please read the next part of my e-mail too. It actually answer your question. > I did read it, and it didn't answer the question. Your desired semantics are not clear. ChrisA ___ Python-ideas maili

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

2023-09-12 Thread Valentin Berlier
Do you mean something like C# nameof()? https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof ___ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mai

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

2023-09-12 Thread Dom Grigonis
An addition to f-strings: a = 1 b = 1 print(f'{a+b}') # '2' print(f'{a+b=}') # 'a+b=2' # I suggest adding this too print(f'{=a+b}')# 'a+b' It is different from putting it in quotes in a way how editors interpret it. E.g. changing the variable name in PyCharm would unambiguously change

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

2023-09-12 Thread Dom Grigonis
Yes, Thank you! So 2 solutions now. They both solve what I have encountered. Beyond that, they differ by: a) f-string print(f’{=a.method}’) # ‘a.method’ No new builtin needed. Simply reprints expression representation. b) print(nameof(a.method)) # ‘method’ New builti

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

2023-09-12 Thread Dom Grigonis
Good points, thanks for reply. > Or do you want to check that the expression can be evaluated at run time? > You could achieve that by simply writing > a.method Yes, the point is that it would be checked for legality of expression itself. Whether to check if it actually evaluates without an e

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

2023-09-12 Thread Rob Cliffe via Python-ideas
As for the example in your first post: var = 710 variable_name = [k fork, v inlocals().items()ifv == 710][0] print("Your variable name is "+ variable_name) it does "work", but it doesn't make much sense with Python's semantics.  You could have two identifiers bound to the same object; which o

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

2023-09-12 Thread Dom Grigonis
I agree. What I meant is that it is unreliable with `v is 710` and even more unreliable with `v == 710` as “==“ is less strict than “is”. DG > On 12 Sep 2023, at 21:03, Rob Cliffe wrote: > > >>> As for the example in your first post: >>> var = 710 >>> variable_name = [k for k, v in locals().

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

2023-09-12 Thread Rob Cliffe via Python-ideas
On 12/09/2023 11:54, Dom Grigonis wrote: Yes, Thank you! So 2 solutions now. They both solve what I have encountered. Beyond that, they differ by: a) f-string print(f’{=a.method}’) # ‘a.method’ No new builtin needed. Simply reprints expression representation. I don't unde