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 obviously require 
an exception in parser itself.
a = object()
b = a
print(a.__varname__)  # ‘a'
print(b.__varname__)  # 'b'
print((a + b).__varname__)  # Undefined??? ‘a + b’?

Actually, it could be easily achieved by what is happening in f-strings:
a = 1
b = 2
print(f'{a+b=}')    # 'a+b=2'
# So I would like to be able to have something like
print(f'{=a+b}')    # 'a+b'
So now it really looks just an addition to f-strings. They can print
1. value
2. `expression literal`=value
3. What I am asking is: `expression literal` (without value)

One can of course argue that this is the same as just typing it, but the 
difference is that things within curly braces are interpreted as code by the 
editor, which is the whole point.

DG.

> On 12 Sep 2023, at 12:24, Antoine Rozo <antoine.r...@gmail.com> wrote:
> 
> 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 <dom.grigo...@gmail.com 
> <mailto:dom.grigo...@gmail.com>> 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] 
> print("Your variable name is " + variable_name)
> 
> ,except robust.
> 
> Possible ways to expose it:
> * builtin function `varname(obj: object) —> str`
> * object.__varname__ dunder (this one wouldn’t clutter general namespace)
> 
> I am working on some code and just got to the point where it seems I could 
> make use of it. The case is as follows:
> 
> # What I do now:
> class A:
>     defaults = dict()
> 
>     def set_default(self, a):
>         self.defaults['a'] = a
> 
>     def make_something(self, a=None):
>         a = a if a is not None else self.defaults.get('a')
>         return Something(a)
> 
> # What I would like to be able to do:
> class A:
>     defaults = dict()
> 
>     def set_default(self, a):
>         self.defaults[a.__varname__] = a
> 
>     def make_something(self, a=None):
>         a = a if a is not None else self.defaults.get(a.__varname__)
>         return Something(a)
> 
> In this case I like second one, because it allows a tight coupling of 
> variable names, which has the following benefits:
> 1. General simplification, where one doesn’t have to think variable name and 
> its key value in dictionary. Code looks cleaner.
> 2. If one was to change a variable name of `set_default` it would immediately 
> surface as an error.
> 3. For someone working with IDEs or making use of clever multiple selection 
> tools, it would be easy to change all in one go.
> 
> Any reasons why the above would not be a good practice?
> Any reasons why exposing it is not a good idea?
> Would this be difficult to achieve from python dev’s perspective?
> 
> 
> — This one can’t be solved with deferred eval :/ —
> Regards,
> DG
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org 
> <mailto:python-ideas@python.org>
> To unsubscribe send an email to python-ideas-le...@python.org 
> <mailto:python-ideas-le...@python.org>
> https://mail.python.org/mailman3/lists/python-ideas.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/H3O6N2W62EDU75JEQQGN63HETAZNANLP/
>  
> <https://mail.python.org/archives/list/python-ideas@python.org/message/H3O6N2W62EDU75JEQQGN63HETAZNANLP/>
> Code of Conduct: http://python.org/psf/codeofconduct/ 
> <http://python.org/psf/codeofconduct/>
> 
> 
> -- 
> Antoine Rozo
> _______________________________________________
> 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/RINOBZMXRJIHY2U7AYKKA5YAXZ5WJNAP/
> 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/7XLHNXNO4CVRRKUI36W5XU6QGDQWQTKQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to