News for hackers. Stuff that matters.
Re:Anti-Patterns in Python Programming by geertj on Wednesday July 09, 2014 @10:36AM (#8009808)
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.
Re:Anti-Patterns in Python Programming by jamiis on Wednesday July 09, 2014 @10:55AM (#8009916)
You 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/mu...
Re:Anti-Patterns in Python Programming by kunstmord on Wednesday July 09, 2014 @01:35PM (#8010992)
In case you're using Python 3, why not write ssl_args: dict, ssl: bool=False?
Re:Anti-Patterns in Python Programming by tribaal on Wednesday July 09, 2014 @10:55AM (#8009914)
Well, that's throwing people implementing subclasses under the bus, IMO.

Why not just add @param annotations in your docstrings instead?

Re:Anti-Patterns in Python Programming by geertj on Wednesday July 09, 2014 @11:32AM (#8010171)
> 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.

Re:Anti-Patterns in Python Programming by Peaker on Wednesday July 09, 2014 @03:02PM (#8011574)
A 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?