Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

f-string syntax deficiency?

9 views
Skip to first unread message

Neal Becker

unread,
Jun 6, 2023, 10:05:09 AM6/6/23
to
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

Chris Angelico

unread,
Jun 6, 2023, 10:08:36 AM6/6/23
to
Is this a problem with the f-string, or with the expression
opt.return? That's a keyword.

ChrisA

Roel Schroeven

unread,
Jun 6, 2023, 10:41:41 AM6/6/23
to
Op 6/06/2023 om 16:08 schreef Chris Angelico:
'return' being a keyowrd is definitely going to be the problem.

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.

(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.)

--
"Don't Panic."
-- Douglas Adams, The Hitchhiker's Guide to the Galaxy

Roel Schroeven

unread,
Jun 6, 2023, 10:47:36 AM6/6/23
to
Op 6/06/2023 om 16:41 schreef Roel Schroeven:
> 'return' being a keyowrd is definitely going to be the problem.
*keyword

Chris Angelico

unread,
Jun 6, 2023, 10:48:55 AM6/6/23
to
On Wed, 7 Jun 2023 at 00:42, Roel Schroeven <ro...@roelschroeven.net> wrote:
> (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:

>>> opt.return
File "<stdin>", line 1
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.

ChrisA

Roel Schroeven

unread,
Jun 6, 2023, 10:56:57 AM6/6/23
to
Op 6/06/2023 om 16:48 schreef Chris Angelico via Python-list:
Ah yes, good point.

Mark Bourne

unread,
Jun 6, 2023, 3:46:59 PM6/6/23
to
Roel Schroeven wrote:
> Op 6/06/2023 om 16:08 schreef Chris Angelico:
>> On Wed, 7 Jun 2023 at 00:06, Neal Becker <ndbe...@gmail.com> wrote:
>> >
>> > 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.
> 'return' being a keyowrd is definitely going to be the problem.
>
> 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.

Just for fun, here's a class which has any attribute you like, including
`return`:
```
class AnyAttr:
def __getattr__(self, name):
return f'This is the value of the <{name}> attribute'
```

The usual "dot" notation to access the attributes works for arbitrary
attributes, but not for `return` because, as mentioned, that's a keyword:
```
>>> aa = AnyAttr()
>>> aa.myattribute
'This is the value of the <myattribute> attribute'
>>> aa.random
'This is the value of the <random> attribute'
>>> aa.return
File "<stdin>", line 1
aa.return
^
SyntaxError: invalid syntax
```

If you really do have an instance with a `return` attribute (perhaps
because it does fun things with `__getattr__`), you can access it if
necessary using `getattr`:
```
>>> getattr(aa, 'return')
'This is the value of the <return> attribute'
```

You can even access attributes with spaces and other usually-invalid
characters:
```
>>> getattr(aa, 'This really is an attribute name!')
'This is the value of the <This really is an attribute name!> attribute'
```

Using `getattr` to access the value, Neal's f-string can be made to work:
```
>>> f'thruput/{"user" if getattr(aa, "return") else "cell"} vs.
elevation\n'
'thruput/user vs. elevation\n'
```

But if you have control over the implementation of that `opt` object,
it's probably better to give the attribute a different name which is a
valid identifier. PEP-8 suggests a convention of using a single
trailing underscore (e.g. `return_`) to avoid conflicts with keywords,
if there's no better name.

Perhaps `opt` is the arguments returned by `argparse.ArgumentParser` and
you want the command-line option to be `--return`. In that case, see
the `dest` argument to `add_argument()` which can specify a different
name for the attribute used in code (it's almost like they thought about
this type of problem ;o)). If it's from `optparse`, that has a similar
argument, but `optparse` is deprecated so consider updating to `argparse`.

>
> (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.)

--
Mark.
0 new messages