[issue38654] `urllib.request.Request` uses mutable value as default value
New submission from Efanzh : 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. -- components: Library (Lib) messages: 355750 nosy: EFanZh priority: normal pull_requests: 16537 severity: normal status: open title: `urllib.request.Request` uses mutable value as default value type: enhancement versions: Python 3.9 ___ Python tracker <https://bugs.python.org/issue38654> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue38654] `urllib.request.Request` uses mutable value as default value
Efanzh added the comment: How about changing `if headers` to `if headers is not None`? -- ___ Python tracker <https://bugs.python.org/issue38654> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue38654] `urllib.request.Request` uses mutable value as default value
Efanzh added the comment: 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”. -- ___ Python tracker <https://bugs.python.org/issue38654> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com