Common Python Mistakes
41–50 of 146 posts
Re: Common Python Mistakes
#42Earlier 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.
Re: Common Python Mistakes
#43For #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.
Re: Common Python Mistakes
#44Another 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.
def x(self, new_value):
self._x = x
really what you mean? not _x = new_value?Re: Common Python Mistakes
#45Earlier 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.
Re: Common Python Mistakes
#46My 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.
Re: Common Python Mistakes
#47Re: Common Python Mistakes
#48This 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…
[Toptal blog editor]
Re: Common Python Mistakes
#49My 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
#50Earlier 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.
print x,
print y
But I generall find myself doing the , in the situation like (element1,) to create a new tuple.