On 2023-09-12 16:54, Rob Cliffe via Python-ideas wrote:


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 understand your semantics either.  What would be the difference between your proposed
     print(f’{=a.method}’)
and simply writing
     print('a.method')
?

Would it be just that the syntax inside the curly braces is checked for legality,
so that
     print(f'{=!?}')
would not be allowed (presumably it would cause a SyntaxError)
?

Or do you want to check that the expression can be evaluated at run time?
You could achieve that by simply writing
     a.method

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 one you got hold of would be essentially random.

I think the point is to have an equivalent to C#'s 'nameof'.

It would be evaluated at compile time to a string, but with the advantage that it's clear that it's a name and not some random string that just happens to look like a name.

For example, if you had, say:

    print('The value of count is', count)

then an IDE wouldn't know that the "count" in the string literal is the name of the variable 'count', whereas in:

    print('The value of', nameof(count), 'is', count)

it's clear that you're giving the name.

An IDE would be able to rename it correctly.

This:

    print('The value of', nameof(count), 'is', count)

would be compiled as:

    print('The value of', 'count', 'is', count)

Tell the IDE to rename 'count' to 'my_count' and you get:

    print('The value of', nameof(my_count), 'is', my_count)

which would be compiled as:

    print('The value of', 'my_count', 'is', my_count)

Telling an IDE to rename when you have:

    print('The value of', 'count', 'is', count)

would give you:

    print('The value of', 'count', 'is', my_count)

It would miss the string literal 'count', and you'd have to fix that by hand.
_______________________________________________
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/SX5PEP3UVDGTUD44RVBZHHKVJRWZOBKX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to