Live data from Hacker News

Common Python Mistakes

toptal.com

101–110 of 146 posts

Re: Common Python Mistakes

#101
post #98

Earlier quoted context omitted.

At which point they will no longer make these mistakes, and thus are more expert. Everyone wins!

Except that you're risking hiring people that know all the trivia but still can't program FizzBuzz properly. The test loses its predictive value.

but at least employers will stop testing trivia and focus on problem solving skills.

Everyone wins!

Re: Common Python Mistakes

#102
post #94

Earlier quoted context omitted.

While I agree that the "problem" case can be seen as obvious when considered in isolation, really it's the behaviour of the two cases taken together that can seem inconsistent. Nothing about understanding OOP or inheritance will prepare a person for that.

>really it's the behaviour of the two cases taken together that can seem inconsistent. Why do you think so? I think that both cases seem consistent, or rather, correct (and therefore this example should not be treated as a common Python mistake), because x is not assigned a value anywhere in class C, and C inherits from A, so it should be clear to anyone knowing OOP and inheritance, that C's x is the same as A's x. (…

What's happening here is that a variable is inheriting its value from the superclass, except for when it doesn't. And when it doesn't, why is that? Well presumably it's because something's been overridden - OO tells us that's how we change the properties that are inherited from the superclass. No wait, that's not it; nothing's been overridden here. All that's happened is we've assigned a value to B.x, and doing so seems to have changed the inheritance of our class.

So this variable is neither completely shared across classes and their subclasses (per Smalltalk class variables), nor completely independent across classes and their subclasses (per Smalltalk class instance variable), but instead its [in]dependence alters based upon whether (and where) you assign values to it.

While I can understand that in terms of the dictionary mechanism used to implement it, from my point of view it's just weird behaviour.

Re: Common Python Mistakes

#103
post #72

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 ]

I totally agree, but if you have javascript folks on your team you run into issues with this... (thanks IE!)

Re: Common Python Mistakes

#105
post #29

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

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

I feel that what most people actually mean when they say "compiled" vs. "interpreted" is whether or not the language specification has additional static checking beyond what is required by parsing. Essentially it is whether the language defers all errors to run-time or attempts to detect classes of them at compile-time. A language like JavaScript accepts as a program any string that parses, while a language like Java rejects many strings based on additional checks such as type rules. You can add static type-checking to JavaScript or Python, but it isn't part of the language spec. You can run C++ or Java with run-time type-checking, but it doesn't conform the to spec. In this way you could say that the language is fundamentally compiled or interpreted.

Of course, this doesn't address issues of incremental evaluation which often requires additional semantics for compiled languages.

Re: Common Python Mistakes

#107
Some of the mistakes mentioned in OP (#1, #3, and #4) can be automatically caught by tools like PyLint (and to lesser extent, Pyflakes), as well as good unittests.

Re: Common Python Mistakes

#109
post #72

Earlier quoted context omitted.

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 ]

I totally agree, but if you have javascript folks on your team you run into issues with this... (thanks IE!)

Coffeescript!
Post reply on HN