Live data from Hacker News

Common Python Mistakes

toptal.com

61–70 of 146 posts

Re: Common Python Mistakes

#61
post #40

Another 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.

is def x(self, new_value): self._x = x really what you mean? not _x = new_value?

You're right, I fixed it.

Re: Common Python Mistakes

#62

There are more than a few interesting points in here but this is funny to me coming from someone who is seemingly well versed in python: Mistake #5 numbers = [n for n in range(10)] this should be: range(10)

not in Python 3, where range() replaces xrange()

interesting, haven't written in python 3 yet

Re: Common Python Mistakes

#63

Earlier quoted context omitted.

The compiled vs. interpreted thing is kind of a historical relic that people still cling on to. It's not dissimilar to where one draws the line for "scripting" or "nth generation" languages; it's all somewhat nebulously defined.

Do people still talk about nth-generation languages outside explaining why Forth is called Forth? I thought it was mostly history by now.

From my experience, the only time I've heard it nowadays is from people who were taught how to code by people who haven't coded since at least the 80s and never went on to develop the skill either professionally or as a hobby. So, yes, very much a historical relic!

Re: Common Python Mistakes

#64

There are more than a few interesting points in here but this is funny to me coming from someone who is seemingly well versed in python: Mistake #5 numbers = [n for n in range(10)] this should be: range(10)

Unless its Python 3.x.

Though, really, even there, while the list comprehension works, its kind of an awkward construction to use that instead of:

  numbers = list(range(10))

Re: Common Python Mistakes

#65

> "Python is an interpreted, object-oriented, high-level programming language with dynamic semantics." I have an issue with that statement. No languages are inherently "compiled" or "interpreted", that's a property of the implementation. If we are talking about CPython here, Python code is compiled to bytecode which is then interpreted. Not unlike Java - with the difference that the main implementation has a JIT and…

That's technically true, but I still think it's reasonable to casually refer to Python as "interpreted," since it conveys useful ideas, although those ideas are more accurately conveyed with different phrasing.

That said, for more precise discussions, what you pointed out is valid and important. One of the early questions I ask in a programming interview is to explain some high-level differences between two languages they're familiar with, which is often Java and Python. One of the common responses I get is that Java compiles to bytecode which is executed by a VM, while Python is interpreted. Of course, I point out that CPython is also compiled to bytecode and executed by a VM.

Re: Common Python Mistakes

#66
post #40

Another 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 :)

Re: Common Python Mistakes

#68
post #49

Slightly off topic, but does anyone know of a resource that has 'most common mistakes' for different languages all in one place? It's certainly possible to google for blog posts and stack overflow questions to assemble such a list, but it would be handy to have them all in one place. My use case is when interviewing candidates I often ask them to rate themselves on a scale of 1-5 in the languages they know, and then…

While such a resource would make your job easier, it would make the interviewee's job easier still. They'd just have to memorize all the points in the reference.

Re: Common Python Mistakes

#69
post #66
post #40

Another 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 :)

Yes, but just because all classes in python 3 are new-style.

Re: Common Python Mistakes

#70
post #39

This is a pretty good list of gotchas, but it's important when writing something targeted at beginners to be as precise and clear as possible. Nearly every section here either uses terminology poorly, is slightly incorrect, or has difficult examples. Python supports optional function arguments and allows default values to be specified for any optional argument. No, specifying a default is what causes an argument to b…

Just to notice: this text is not targeted at beginners, in my opinion. These are upper-intermediate to advanced level gotchas.
Post reply on HN