Skip to content

docs: Specify preferred Python string formatting technique - #13718

Merged
maflcko merged 1 commit into
bitcoin:masterfrom
masonicboom:python-string-format-guideline
Jul 20, 2018
Merged

maflcko merged 1 commit into
bitcoin:masterfrom
masonicboom:python-string-format-guideline

Conversation

@masonicboom

Copy link
Copy Markdown

@practicalswift this documents the nit you suggested in my previous PR
#13707 (comment)

@practicalswift

practicalswift commented Jul 19, 2018 •

Copy link
Copy Markdown
Contributor

Regarding my suggestion in #13707 (comment): it should be noted that a nit is just a suggested change for a trivial often non-blocking issue. Not all style nits end up in the developer notes (due to various reasons: no clear consensus, not worth documenting, etc.).

With that out of the way I'm ACK:ing this PR:

The Python documentation is discouraging the use of the old string formatting style ('%s' % x):

4.7.2. printf-style String Formatting
Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.

From older Python 3 documentation about '%s' % x:

5.6.2. Old String Formatting Operations
Note: The formatting operations described here are obsolete and may go away in future versions of Python. Use the new String Formatting in new code.

ACK b1b8863

Trivia: When we move to Python 3.6 we can start using formatted string literals ("f-strings") as well :-)

@masonicboom

Copy link
Copy Markdown
Author

Thanks. My philosophy is: if it's worth a nit, it's worth documenting (or even better, automating with lint rules) to eliminate review round-trips in the future.

Trivia: When we move to Python 3.6 we can start using formatted string literals ("f-strings") as well :-)

Nice!

@practicalswift

Copy link
Copy Markdown
Contributor

@masonicboom I share your love for automation/linting! 🏧😍

@laanwj

laanwj commented Jul 20, 2018 •

Copy link
Copy Markdown
Member

Concept ACK, no strong feelings on what formatting to use, and please do not create PRs that change this all over the place, but for new code it is a good idea to use the new, recommended style consistently.

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code

I really like that! string-formatting as a macro-expression. But it will be a while before 3.6 can be required as minimum without breaking a lot of builds. 3.6.0 was released at the end of 2016.

@maflcko

maflcko commented Jul 20, 2018

Copy link
Copy Markdown
Member

Agree with @practicalswift and @laanwj

it should be noted that a nit is just a suggested change for a trivial often non-blocking issue. Not all style nits end up in the developer notes (due to various reasons: no clear consensus, not worth documenting, etc.).

@maflcko
maflcko merged commit b1b8863 into bitcoin:master Jul 20, 2018
maflcko pushed a commit that referenced this pull request Jul 20, 2018
b1b8863 docs: Specify preferred Python string formatting technique (Mason Simon)

Pull request description:

  @practicalswift this documents the nit you suggested in my previous PR
  #13707 (comment)

Tree-SHA512: b75335976b30ccbce5c2e3a46e5b6218e797944e7025c17b4e56f294670397d731e1a9f4fe7e809d907136e2e62cd9c2b8404b875b4879a1e3f0b1970b94d5a8
@promag

promag commented Jul 20, 2018

Copy link
Copy Markdown
Contributor

ACK

PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 17, 2020
… technique

b1b8863 docs: Specify preferred Python string formatting technique (Mason Simon)

Pull request description:

  @practicalswift this documents the nit you suggested in my previous PR
  bitcoin#13707 (comment)

Tree-SHA512: b75335976b30ccbce5c2e3a46e5b6218e797944e7025c17b4e56f294670397d731e1a9f4fe7e809d907136e2e62cd9c2b8404b875b4879a1e3f0b1970b94d5a8
maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Nov 18, 2020
97c738f [tests] Recommend f-strings for formatting, update feature_block to use them (Anthony Towns)
8ae9d31 Bump minimum python version to 3.6 (Anthony Towns)

Pull request description:

  Python 3.5 has reached [end-of-life](https://devguide.python.org/#status-of-python-branches) as of September 2020, and 3.6 has some moderately nice [features](https://docs.python.org/3/whatsnew/3.6.html):

  - `f'x = {x}'` as an alternative to `'x = {}'.format(x)` format strings (cf bitcoin/bitcoin#13718 (comment))
  - underscore separators for large numbers, like `1_234_567`
  - improvements to async
  - improvements to typing module

  Note that 3.6 is not available in xenial (16.04), but is available in bionic (18.04), while focal (20.04) has 3.8. CentOS 7 and 8 have 3.6.8, Debian stable has 3.7.3, and [gentoo and arch already had 3.6 and 3.7 in 2018](bitcoin/bitcoin#14954 (comment)).

ACKs for top commit:
  MarcoFalke:
    re-ACK 97c738f

Tree-SHA512: ec7fce68845edde4d61a42de12c065fd49e5217311a6fda1323206f091a0afd50f293645dffc27d420127e4e5deb864e953f1b67eff735a0dfbbedd7899a9d60
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants