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

2023-09-16 Thread Dom Grigonis
Hi Jonathan, I wasn’t writing anything specialised on handling of defaults. My initial example pretty much covers it. What defaults handler of python are you talking about? This proposal is more about convenient refactoring and keeping things nicely synchronised where variable name and string

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

2023-09-16 Thread Jonathan Fine
Hi Dom In your original post you said you're writing some code to improve handling of defaults (perhaps only in some situations). You also said that the feature you're suggesting would make your improved default handler easier to write. Python already handles default values at least fairly well i

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

2023-09-16 Thread Dom Grigonis
Agree, all good points. f'{=expr}'.split('.')[-1] Does take care of all cases and `nameof` has no additional use over it. It seems that `nameof` is out of question. DG > On 16 Sep 2023, at 19:50, Stephen J. Turnbull > wrote: > > Dom Grigonis writes: > >> print(f'{=A.a}')# 'A.a' >> print(

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

2023-09-16 Thread Stephen J. Turnbull
Dom Grigonis writes: > print(f'{=A.a}')# 'A.a' > print(nameof(A.a)) # 'a' I see that's the C# semantics, but it's not obvious to me why the ambiguity introduced is worth emulating. The important aspect of the proposed 'nameof' operator is that its argument can be validated by the compiler

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

2023-09-16 Thread Dom Grigonis
If it is done at a tool level, it is not a robust solution. The fact that it can be done in a tool doesn’t mean that it is the optimal solution. Having it done at python level could be a simpler solution and provide benefits to all of the tools, without needing for editor logic to deviate from p

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

2023-09-16 Thread Bruce Leban
If the goal is to create a syntax that allows tools to recognize names in strings, there is a simple solution which requires tool changes only, for example: raise Exception('The field ' + ((('size'))) + ' must be a positive integer in ' + ((('Sample.widget' Python already treats triple parent

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

2023-09-16 Thread Chris Angelico
On Sat, 16 Sept 2023 at 19:48, Jeff Allen wrote: > It needs language (parser) support because it does what you identified early > on: "take whatever is given and put it in quotes", where "whatever is given" > is the text of the expression only available at run-time. > I didn't say "and put it i

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

2023-09-16 Thread Jeff Allen
On 16/09/2023 11:33, Dom Grigonis wrote: It works already with existing tools that will treat "nameof(a+b)" as a function call, which it is syntactically but not semantically, so renaming b to c will now produce the string "a+c". I think my IDE does offer to do this with variable names in str

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

2023-09-16 Thread Dom Grigonis
Yes, it seems it does have scope beyond editor in both cases. class A: a = 1 print(f'{=A.a}')# 'A.a' print(nameof(A.a)) # 'a' In f-string case, if to be consistent with current f-string functionality, expression would be checked for a validity and that reference is in scope and available.

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

2023-09-16 Thread Jeff Allen
On 16/09/2023 01:14, Chris Angelico wrote: But if that tool can be taught that nameof indicates a variable name, then it can rename inside it just like it would rename anything else. It works already with existing tools that will treat "nameof(a+b)" as a function call, which it is syntactically