Live data from Hacker News

Common Python Mistakes

toptal.com

41–50 of 146 posts

Re: Common Python Mistakes

#42
post #36
post #21

Earlier quoted context omitted.

Doing (x, y) = (5, 6) makes it clearer to "visually grep" that it's not a normal assignment though.

It's the comma that determines an expression as a tuple, not the parentheseis.

Which is the source of the original problem, which I've also encountered myself.

Re: Common Python Mistakes

#43

For #7, now you have a performance problem of importing every time you run that function. Rather, you can place the import at the bottom of b.py and be okay.

Python caches modules imported, you can check it out in your local shell with: import sys ; sys.modules. That's why whenever you make changes to a module which has been already loaded you won't see the changes until you load the module again, either quitting the shell or using reload(module) on Python 2.x

Re: Common Python Mistakes

#44
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?

Re: Common Python Mistakes

#45
post #29

Earlier quoted context omitted.

> No languages are inherently "compiled" or "interpreted", that's a property of the implementation. A language and it's implementation are usually designed at the same time. Compiled or interpreted will affect design choices that go into the language. While additional implementations may follow, it can be hard/impossible to design a compiler (machine code, not byte code) for a language that was designed to be interpr…

Not really - Javascript was designed to be interpreted, and yet V8/SpiderMonkey/Nitro all JIT-compile it down to machine code, sometimes very effectively.

[deleted]

Re: Common Python Mistakes

#46
post #2

My pet peeve with python is the classic: return x, I have never wanted to declare a tuple without surrounding it with (). Too bad it's not a syntax error in python 3. Also, as opposed to one of his examples, if you are using python 2.7, declare your exception blocks as: except (FooException, BarException) as e: It's forward compatible with python 3, it's easier to read and the syntax errors are clearer.

You should be able to easily add an extra syntax rule to your editor or git pre-commit (/return.*,$/ type of thing if this bothers you too much...

Re: Common Python Mistakes

#48
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…

Hey, thanks for the great feedback! We agreed with (almost :-) ) all of your comments and have made corresponding mods/corrections to the post. Thanks again!

[Toptal blog editor]

Re: Common Python Mistakes

#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 ask them increasingly 'tricky' questions in each language to get a feel for how their "personal" scale aligns to their real knowledge. This works fine if we have an overlap of several languages, but in the case where I know nothing or very little of one of the languages they know I lose that data point.

I find it valuable to know what a "I am a 1 at X" vs "I am a 3 X" vs "I am a 5 at X" means to them, since I've found little correlation between how harshly someone rates themselves and their true ability. Sometimes self-rated 5s are really 5s by my book, sometimes self-rated 3s are really 5s by my book, and sometimes self-rated 5s are really 2s by my book. So I want to know how "my scale" translates to "their scale". If it was more formalized I'd go as far as to get a "confidence quotient" for a person as self-critical and self-confident people can be fantastic engineers or horrible engineers.

Does anyone else do this process when interviewing?

Re: Common Python Mistakes

#50
post #36
post #21

Earlier quoted context omitted.

Doing (x, y) = (5, 6) makes it clearer to "visually grep" that it's not a normal assignment though.

It's the comma that determines an expression as a tuple, not the parentheseis.

That syntax reminds me of trying to do the following

    print x,
    print y
But I generall find myself doing the , in the situation like (element1,) to create a new tuple.
Post reply on HN