4 ms·
This is a pretty minor thing compared to a lot of the warts here - to be honest, I'm not in the league of most of you as a programmer, I suspect - but two prett
by adw 17y ago
This is a pretty minor thing compared to a lot of the warts here - to be honest, I'm not in the league of most of you as a programmer, I suspect - but two pretty simple things I've found surprising/annoying and have managed to shoot myself in the foot with:
* loops leaking their control variables: I get why this is useful sometimes, but it particularly bites with list comprehensions, especially as generator expressions don't leak. Apparently the list-comprehension part of this goes away in Python 3 (http://mail.python.org/pipermail/python-3000/2008-April/013189.html http://mail.python.org/pipermail/python-3000/2008-April/0131...).
* How mutable default argument values behave. Now, this one's even mentioned in the tutorial - http://docs.python.org/tutorial/controlflow.html#default-argument-values http://docs.python.org/tutorial/controlflow.html#default-arg... - so I've really got no excuse, but that hasn't stopped me getting it wrong before!
- jnoller 17y agoMutable arguments are considered harmful; and will probably cause cancer, plague, locusts and other end-of-the-world problems.
- j_baker 17y agoI was under the impression that Python's handling of mutable arguments was actually a feature. Although I must admit, I've never written any code that would use it.
- adw 17y agoYeah, that's more or less what happens. What you'd, admittedly naively, want, though, is a way of saying "a new empty dictionary, please", so you don't wind up having to write this kind of thing: def f(a, d=None): if d is None: d = {} which, okay, it's a minor deal, and I know why it happens, but it's the kind of thing I forget you need to do!