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: `urllib.request.Request` uses mutable value as default value
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: EFanZh, serhiy.storchaka, steven.daprano
Priority: normal Keywords:

Created on 2019-10-31 13:17 by EFanZh, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17007 closed EFanZh, 2019-10-31 13:17
Messages (7)
msg355750 - (view) Author: EFanZh (EFanZh) * Date: 2019-10-31 13:17
The `headers` argument of the `__init__` method of `urllib.request.Request` class uses `{}` as default value, which is mutable. It is not a good idea.
msg355757 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-31 15:10
There is nothing wrong with using {} as default value.
msg355765 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-10-31 20:14
I agree with Serhiy that using mutable defaults is not automatically a bad idea. This is unnecessary code churn that fixes no bugs and adds no new functionality and doesn't make the code "better".

The PR removes one harmless use of a mutable default but adds the "code smell" that assumes that None is the only possible falsey value. This means that the ``__init__`` method will accept any falsey value without complaint:

    Request(url, headers=0.0)

which is worse than the current code.
msg355774 - (view) Author: EFanZh (EFanZh) * Date: 2019-10-31 23:10
How about changing `if headers` to `if headers is not None`?
msg355775 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-31 23:11
It is not needed. There is no bug. Nothing to change.
msg355778 - (view) Author: EFanZh (EFanZh) * Date: 2019-11-01 01:46
I agree that this doesn’t fix any bug. But is is easy to make mistakes by using mutable default values (See https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments). Both PyCharm and pylint show warnings about mutable default arguments even there is no bug in the code.

Using `{}` as default value, by looking at the function signature, I might wonder whether this argument stays the same on each function call, since it is mutable. But by using `None` as default value, I am know this argument will always be `None` on each function call. Using `None` as default value makes the function interface more easy to understand.

Also, if some new Python coders saw `[]` or `{}` being used as default values in the standard library, they might think “I’ll do it too since the standard library does it”.
msg355817 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-11-01 13:58
> Also, if some new Python coders saw `[]` or `{}` being used as default 
> values in the standard library, they might think “I’ll do it too since 
> the standard library does it”.

Great! Having Python coders learn good progamming skills from the 
standard library is a *good* thing.
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82835
2019-11-01 13:58:36steven.dapranosetmessages: + msg355817
2019-11-01 01:46:44EFanZhsetmessages: + msg355778
2019-10-31 23:11:32serhiy.storchakasetstatus: open -> closed
resolution: not a bug
messages: + msg355775

stage: resolved
2019-10-31 23:10:14EFanZhsetmessages: + msg355774
2019-10-31 20:14:45steven.dapranosetnosy: + steven.daprano
messages: + msg355765
2019-10-31 15:10:41serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg355757
2019-10-31 13:17:53EFanZhcreate