This list is an excellent summary. If tasked with a #11 I'd probably add the slightly more obscure, but still super painful (when you do run into it) implicit string concatenation: >>> l = ["a", ... "b", ... "c" ... "d"] >>> l ['a', 'b', 'cd']
Still hugely useful when you want to write long string constants. Imho, adding a comma after each list element is a good practice. You can easily swap them, add more, and never run into a an issue you describe: foo = [ "a", "bc", "def", # comma here, too ]
Common Python Mistakes
81–90 of 146 posts
Re: Common Python Mistakes
#82Earlier quoted context omitted.
Not really - Javascript was designed to be interpreted, and yet V8/SpiderMonkey/Nitro all JIT-compile it down to machine code, sometimes very effectively.
When people talk about "compiled" vs. "interpreted" they usually mean AOT compilation, not JIT.
I think this just illustrates the fuzziness of these definitions. A compiler is just a piece of code; you can embed it into another piece of code and run it whenever necessary. Maybe in the world of shrink-wrapped desktop software there was a sharp distinction between AOT compiled languages and interpreted ones, but we haven't lived in that world for a couple decades now.
Re: Common Python Mistakes
#83Earlier quoted context omitted.
Just to notice: this text is not targeted at beginners, in my opinion. These are upper-intermediate to advanced level gotchas.
Did they add in "(Note: This article is intended for a more advanced audience than Common Mistakes of Python Programmers, which is geared more toward those who are newer to the language.)" later? Because it states it right near the top of the article.
Re: Common Python Mistakes
#84Earlier quoted context omitted.
It's the comma that determines an expression as a tuple, not the parentheseis.
Not all the time. x = , causes a syntax error, while x = (,) creates an empty tuple.
>>> x=(,)
File "", line 1
x=(,)
^
SyntaxError: invalid syntaxRe: Common Python Mistakes
#85Re: Common Python Mistakes
#86Here’s a thought experiment for you—think of “common mistakes in language X” as “design flaws in language X” or “ways in which language X is surprising” and what could have been done to mitigate that.
Whether you choose 0-based indexing or 1-based indexing, somebody is going to be confused about indexing sometime.
Re: Common Python Mistakes
#87Earlier quoted context omitted.
In the particular case of constructing several similar functions that do essentially the same thing, lambda is a rather natural choice. Being more explicit and less hacky can well be combined with staying true to functional style: from functools import partial mul = lambda x, y: x*y # could use int.__mul__, too multipliers = [partial(mul, n) for n in range(5)] It does the closure-capturing of n for you.
A function defined with def would work just the same there. It would have a big ugly 'return' in it and be a few characters longer, but it would work the same, so I don't see what lambda brings to it.
Re: Common Python Mistakes
#88In "Common Mistake #2", I'd say that the mistake is fairly obvious to anyone who understands even a little bit about OOP and inheritance. Since class C doesn't define its own variable x, it has to be that it inherits the x in class A, so there's no reason to be surprised that C.x changes when A.x does.
Re: Common Python Mistakes
#89Earlier quoted context omitted.
Not all the time. x = , causes a syntax error, while x = (,) creates an empty tuple.
Under both Python 2.7 and Python 3.3 I get: >>> x=(,) File " ", line 1 x=(,) ^ SyntaxError: invalid syntax
> Empty tuples are constructed by an empty pair of parentheses;
And it works fine
>>> x = ()
>>> x
()
I guess Herge mean that? After all, his argument was that a tuple is not always defined by the comma.Re: Common Python Mistakes
#90Another one: class A(): def __init__(self): self._x = 0 @property def x(self): return self._x @x.setter def x(self, new_value): self._x = new_value Using it: a = A() print a._x # 0 print a.x # 0 a.x = 4 print a.x # 4 print a._x # 0 wait WTF?! The bug was not having `A` inherit from `object`. With old-style classes, properties do not work correctly.
For anyone wondering, this works just fine in Python 3 :)