question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forbid using `f` strings

See original GitHub issue

I don’t see any reasons why we should use them. Let’s use .format() instead.

P.S. For a reason we can not forbid % usage: https://github.com/gforcada/flake8-pep3101/issues/23

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:20
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

11reactions
sobolevncommented, Jul 3, 2018

.format() can be short too:

def some(self):
     return '{0} with {1}'.format(name, item)

But, for me being shorter is not a valid argument. What matters is:

  1. readability
  2. performance
  3. consistency

Readability

My old habits put .format() over f, since I am just used to it. This is highly subjective, but that’s my point.

But, what really makes a difference is that we can not put logics inside .format(), while we can do bizarre things inside f strings:

>>> f'{random.randint(1, 12)}'
'3'

That is so wrong, that I can not allow this.

Performance

Yes, f is slightly faster than .format(). But it is safe to say that they are almost the same.

Consistency

There should be one-- and preferably only one --obvious way to do it. © Tim Peters

I don’t see any use-cases of f strings that can not be covered with .format(). So, let’s stick to a single way to format strings.

3reactions
kxepalcommented, Jul 3, 2018

I guess, @sobolevn really wants just linters for f-strings code and some heuristic to forbid doing complex things inside f-strings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pylint: How to fix "c0209: formatting a regular string which ...
Learn how to fix this new Pylint rule that raises: 'formatting a regular string which could be a f-string (consider-using-f-string)' error.
Read more >
consider-using-f-string / C0209 - Pylint
Description: Used when we detect a string that is being formatted with format() or % which could potentially be a f-string. The use...
Read more >
Python3 f-strings: how to avoid having to escape literal curly ...
I am aware that one can use the older syntax of e.g. div {color: %s} % color , 'text {}'.format(abc) or string templates...
Read more >
PEP 498 – Literal String Interpolation
F -strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is...
Read more >
When not to use f-strings? - Python Forum
format method: PyFormat: Using % and .format() for great good!. No f-strings but I believe it can be source of some ideas. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found