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.

classification
Title: doc: Fix bisect example using mutable function default
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Dennis Sweeney, docs@python, imomaliev, rhettinger
Priority: normal Keywords: patch

Created on 2022-01-02 08:46 by imomaliev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30322 closed imomaliev, 2022-01-02 08:46
Messages (5)
msg409490 - (view) Author: Sardorbek Imomaliev (imomaliev) * Date: 2022-01-02 08:46
Currently, in `bisect` examples `grade` function is declared with mutable default for `breakpoints` arugment. I suggest updating.

https://github.com/sweeneyde commented
> I would say that even though using mutable defaults is often undesirable, there is no problem with it in this case: the body of the function does not mutate the default. It's also probably faster than re-constructing a new list at each function call. I would suggest opening an issue on https://bugs.python.org/ if you want to discuss more.

I agree that in this case it wouldn't matter because `breakpoints` are not mutated, but most of the time people copy from examples and adapt the code to their needs without giving it a second thought. In my opinion, it is a good practice to provide examples as foolproof as possible.
msg409500 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-01-02 15:51
Another option would be to use globals:

>>> BREAKPOINTS = [60, 70, 80, 90]
>>> GRADES = "FDCBA"
>>> def grade(score):
...     i = bisect(BREAKPOINTS, score)
...     return GRADES[i]
... 
>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
['F', 'A', 'C', 'C', 'B', 'A', 'A']
msg409502 - (view) Author: Sardorbek Imomaliev (imomaliev) * Date: 2022-01-02 16:21
It was changed from that in 2010

https://github.com/python/cpython/commit/20933e08b12b4e66c6e1baf662a679b4008d9dce
msg409516 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-01-02 19:59
I'm going to decline this one.  It seems that you're applying a stylistic guideline to a case where it isn't needed and where it doesn't improve the example.

In this case, the example doesn't mutate the input, so the code is correct.  It has some benefit in that it communicates to the user of the function that lists are allowable as an input (which is the norm for bisect).  And the tooltips will show the relationship between the grades can the cutpoints.

Also, this isn't library code.  A bisect example needs to focus on bisect rather than other concerns.  This isn't the place to talk about the pattern of setting a default to None and then filling it in in the body of the code.  Likewise, we wouldn't use this example to communicate / and * for positional-only and keyword-only args.  Nor do we have a docstring or type signature.  Instead, it shows how to write a lookup function using bisect() and that is all we want.

Lastly, I'll note this example has been present for a long time and has proven itself effective in teaching people how bisect works.  In particular, we want to make it obvious the relationship between the 5 grades and the 4 cut points.
msg409558 - (view) Author: Sardorbek Imomaliev (imomaliev) * Date: 2022-01-03 05:18
Understood, thanks for clarification.
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90382
2022-01-03 05:18:46imomalievsetmessages: + msg409558
2022-01-02 19:59:25rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg409516

stage: patch review -> resolved
2022-01-02 19:44:30rhettingersetassignee: docs@python -> rhettinger
2022-01-02 16:22:40Dennis Sweeneysetnosy: + rhettinger
2022-01-02 16:21:47imomalievsetmessages: + msg409502
2022-01-02 15:51:20Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg409500
2022-01-02 09:05:33AlexWaygoodsetkeywords: + patch
stage: patch review
versions: - Python 3.6, Python 3.7, Python 3.8
2022-01-02 08:46:54imomalievcreate