[issue38654] `urllib.request.Request` uses mutable value as default value

Efanzh report at bugs.python.org
Thu Oct 31 21:46:44 EDT 2019


Efanzh <efanzh at gmail.com> 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 <report at bugs.python.org>
<https://bugs.python.org/issue38654>
_______________________________________


More information about the Python-bugs-list mailing list