3 ms·
The Little Book of Python Anti-Patterns
- wingi 12y agoChapter performance, use set instead list: If you convert a complete list to a set only to check if a values in a list this is not performant. Use list.index or binary search instead. But for every performance example you should offer a micro benchmark example.
- ThePhysicist 12y agoyou're absolutely right, will point this out in the next version. As said before, if you think something could be improved, feel free to contribute!
- a3n 12y agoAlthough I mostly agree with "Returning more than one variable type from function call," I do have some disagreement. On the restriction itself, I'm fine with returning, for example, any kind of iterable, whether it's a list, tuple, string or whatever. It depends on context, and python is very much an interface/context kind of language. As for the anti-example given, that code would not fail in most print contexts: In [1]: a = None In [2]: print a None A better anti-example might have been: In [3]: for i in a: ...: print i ...: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-6fc9adb94b0c> in <module>() ----> 1 for i in a: 2 print i 3 TypeError: 'NoneType' object is not iterable
- ThePhysicist 12y agogood point, will change this!
- wingi 12y agoSecond comment: Would you switch to python3 syntax or annotate the examples with python2/python3 differences?
- ThePhysicist 12y agogood point, definitely will do this. If you have any ideas feel free to contribute!