Live data from Hacker News

Common Python Mistakes

toptal.com

51–60 of 146 posts

Re: Common Python Mistakes

#51
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.

When people talk about "compiled" vs. "interpreted" they usually mean AOT compilation, not JIT.

Re: Common Python Mistakes

#52
I have just been asked by a colleague about yet another gotcha when you have for example:

    # mypackage/__init__.py
    from .settings import settings
and when trying to import settings from mypackage

    from mypackage import settings
you get module instead of settings object.

Re: Common Python Mistakes

#53

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']

I never knew you could do that. You don't need the continuation though.

>>> l = ["A", "c""d"] >>> l ['A', 'cd']

Re: Common Python Mistakes

#54
post #52

I have just been asked by a colleague about yet another gotcha when you have for example: # mypackage/__init__.py from .settings import settings and when trying to import settings from mypackage from mypackage import settings you get module instead of settings object.

from mypackage.settings import settings

Re: Common Python Mistakes

#55
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.

Not all the time.

    x = ,
causes a syntax error, while

    x = (,)
creates an empty tuple.

Re: Common Python Mistakes

#56
post #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]

Good changes. One more issue (I believe recently introduced): LEGB ends with "Built-in", not with "Module". It's also good to note in the blog post that it's been updated.

Re: Common Python Mistakes

#57

> "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…

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.

Re: Common Python Mistakes

#58
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)

Re: Common Python Mistakes

#59

> "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…

I think its reasonable to refer to the reference implementation of Python (CPython) as just "Python". The compilation to bytecode is an intermediate step because Python specifies a virtual machine. The language is definitely interpreted though. Its completely accurate to call Python an interpreted language. There's no version of Python that I know of (including PyPy) that ever produces compiled machine executable binaries prior to runtime.

Re: Common Python Mistakes

#60

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()
Post reply on HN