9 ms·
Agreed on all counts. However I do find myself using mutables as default arguments sometimes because the generated documentation is clearer. For example, this
by geertj 12y ago
Agreed on all counts. However I do find myself using mutables as default arguments sometimes because the generated documentation is clearer.
For example, this is a real method in one of my projects:
def listen(self, address, ssl=False, ssl_args={}):
pass
I like the way this turns up in the docs because it's immediately clear that ssl_args needs to be a dict. Otherwise I have to describe it in words.
- tribaal 12y agoWell, that's throwing people implementing subclasses under the bus, IMO. Why not just add @param annotations in your docstrings instead?
- geertj 12y ago> Well, that's throwing people implementing subclasses under the bus, IMO. If they need to touch this argument in an overridden method and they don't know what they are doing, then yes. > Why not just add @param annotations in your docstrings instead? I'm using Sphinx and it renders them separately. I want the empty dict to show up in the function signature.
- Peaker 12y agoA single cryptic bug due to this practice will more than negate the minor doc readability benefits you get from that. And there will likely be many more than one cryptic bug. There are other ways to emphasize it ought to be a dict/mappable. Change its name to be suffixed as "_dict", for example?
- jamiis 12y agoYou should not ever use mutable default args. The documentation benefit is drastically outweighed by the potential for nasty bugs. http://pythonconquerstheuniverse.wordpress.com/2012/02/15/mutable-default-arguments/ http://pythonconquerstheuniverse.wordpress.com/2012/02/15/mu...
- kunstmord 12y agoIn case you're using Python 3, why not write ssl_args: dict, ssl: bool=False?
- deleted 12y ago[deleted]