This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients dmitriym, docs@python, eric.smith, steven.daprano
Date 2019-07-06.01:14:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1562375652.92.0.434565471498.issue37512@roundup.psfhosted.org>
In-reply-to
Content
Eric is correct that this is a CPython optimization, it is not a language feature.

Furthermore, it is an optimization that can be affected by rather subtle factors such as the operating system memory management. Here is a thread demonstrating that code that relied on this optimization ran fast on Linux and slow on Windows, literally hundreds of times slower than other tools:

https://mail.python.org/archives/list/python-dev@python.org/thread/EQO6HDZXHFCP4DBEE5Q7MYLHPJMGJ7DQ/

If you prefer the old mailman archives, here are a few relevant posts:

https://mail.python.org/pipermail/python-dev/2009-August/091125.html

https://mail.python.org/pipermail/python-dev/2009-September/091582.html

https://mail.python.org/pipermail/python-dev/2009-September/091592.html

Best practice is to *not* rely on this optimization, but to use str.join. That ensures that:

- your code remains fast if run on alternate implementations;
- your code remains fast if the OS memory management changes;
- your code remains fast if you change your code slightly.

For example, I expect both of the following to show the documented quadratic behaviour:

- Prepending instead of appending

- keeping two or more references to the string being appended to

  partials = []
  current = ''
  for s in list_of_strings:
      partials.append(current)
      current += s

There may be others.

Perhaps we ought to document this optimization specifically so we can suggest that it is intended for improving performance of casual scripts, and that *best practice* for professional quality code remains str.join.
History
Date User Action Args
2019-07-06 01:14:12steven.dapranosetrecipients: + steven.daprano, eric.smith, docs@python, dmitriym
2019-07-06 01:14:12steven.dapranosetmessageid: <1562375652.92.0.434565471498.issue37512@roundup.psfhosted.org>
2019-07-06 01:14:12steven.dapranolinkissue37512 messages
2019-07-06 01:14:12steven.dapranocreate