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
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
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
>
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
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
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
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
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
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
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
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
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
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().
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
14 matches
Mail list logo