The following f-string does not parse and gives syntax error on 3.11.3:
f'thruput/{"user" if opt.return else "cell"} vs. elevation\n'
However this expression, which is similar does parse correctly:
f'thruput/{"user" if True else "cell"} vs. elevation\n'
I don't see any workaround. Parenthesizing doesn't help:
f'thruput/{"user" if (opt.return) else "cell"} vs. elevation\n'
also gives a syntax error
'return' being a keyowrd is definitely going to be the problem.*keyword
(Recently there has been an effort to provide clearer and more useful
error messages; this seems to be a case where there is still room for improvement: "SyntaxError: invalid syntax" doesn't immediately remind me
of that fact that 'return' is a keyword and therefor can't be used as an attribute.)
File "<stdin>", line 1opt.return
On Wed, 7 Jun 2023 at 00:06, Neal Becker <ndbecker2@gmail.com> wrote:'return' being a keyowrd is definitely going to be the problem.
The following f-string does not parse and gives syntax error on 3.11.3:
f'thruput/{"user" if opt.return else "cell"} vs. elevation\n'
However this expression, which is similar does parse correctly:
f'thruput/{"user" if True else "cell"} vs. elevation\n'
I don't see any workaround. Parenthesizing doesn't help:
f'thruput/{"user" if (opt.return) else "cell"} vs. elevation\n'
also gives a syntax error
Is this a problem with the f-string, or with the expression
opt.return? That's a keyword.
On Wed, 7 Jun 2023 at 00:42, Roel Schroeven <roel@roelschroeven.net> wrote:Ah yes, good point.
(Recently there has been an effort to provide clearer and more useful
error messages; this seems to be a case where there is still room for improvement: "SyntaxError: invalid syntax" doesn't immediately remind me
of that fact that 'return' is a keyword and therefor can't be used as an attribute.)
That's true, but depending on exactly how you're seeing the error, it
might highlight the exact part that's a problem:
File "<stdin>", line 1opt.return
opt.return
^^^^^^
SyntaxError: invalid syntax
It's extremely hard to guess what the programmer might have intended
in these situations, as the error might not be the word "return" but
perhaps the punctuation (maybe that was supposed to be a semicolon, or something). So Python does the best it can, and points out that the
"return" keyword in this context doesn't make syntactic sense.
Op 6/06/2023 om 16:08 schreef Chris Angelico:
On Wed, 7 Jun 2023 at 00:06, Neal Becker <ndbecker2@gmail.com> wrote:'return' being a keyowrd is definitely going to be the problem.
The following f-string does not parse and gives syntax error on 3.11.3:
f'thruput/{"user" if opt.return else "cell"} vs. elevation\n'
However this expression, which is similar does parse correctly:
f'thruput/{"user" if True else "cell"} vs. elevation\n'
I don't see any workaround. Parenthesizing doesn't help:
f'thruput/{"user" if (opt.return) else "cell"} vs. elevation\n'
also gives a syntax error
Is this a problem with the f-string, or with the expression
opt.return? That's a keyword.
Neal, I assume you're using 'opt.return' also outside of that f-string.
Does that work? How did you manage to do that? I tried to make a simple
class with an attribute called 'return', but that already fails with a
syntax error.
'This is the value of the <myattribute> attribute'aa = AnyAttr()
aa.myattribute
'This is the value of the <random> attribute'aa.random
File "<stdin>", line 1aa.return
'This is the value of the <return> attribute'getattr(aa, 'return')
'This is the value of the <This really is an attribute name!> attribute'getattr(aa, 'This really is an attribute name!')
elevation\n'f'thruput/{"user" if getattr(aa, "return") else "cell"} vs.
(Recently there has been an effort to provide clearer and more useful
error messages; this seems to be a case where there is still room for improvement: "SyntaxError: invalid syntax" doesn't immediately remind me
of that fact that 'return' is a keyword and therefor can't be used as an attribute.)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 04:03:42 |
Calls: | 10,386 |
Calls today: | 1 |
Files: | 14,057 |
Messages: | 6,416,603 |