Live data from Hacker News

Common Python Mistakes

toptal.com

71–80 of 146 posts

Re: Common Python Mistakes

#71
post #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']

You don't need the continuation, but that's where you're most likely to make the mistake. It'll catch you when you leave off the comma from the last element of the list, then go back later and add another element to the end.

Re: Common Python Mistakes

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

Re: Common Python Mistakes

#73
post #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.

Did they add in "(Note: This article is intended for a more advanced audience than Common Mistakes of Python Programmers, which is geared more toward those who are newer to the language.)" later?

Because it states it right near the top of the article.

Re: Common Python Mistakes

#74
post #56
post #48

Earlier quoted context omitted.

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 " B uilt-in", not with " M odule". It's also good to note in the blog post that it's been updated.

Doh. You sure it doesn't stand for "Mbuilt-in"? ;-)

Again, thanks for the attention to detail. We've fixed this as well.

Re: Common Python Mistakes

#75

Earlier quoted context omitted.

Agreed 100%, this type of constructions should be avoided in the first place in favor of more "readable" ones but this happens in a fair amount of code that I've seen (and I keep seeing).

Some of it seems to come from people cargo-culting their knowledge of anonymous and first class functions, so they end up believing that the only way to pass a function around is to construct it anonymously.

In the particular case of constructing several similar functions that do essentially the same thing, lambda is a rather natural choice.

Being more explicit and less hacky can well be combined with staying true to functional style:

    from functools import partial
    
    mul = lambda x, y: x*y  # could use int.__mul__, too
    multipliers = [partial(mul, n) for n in range(5)]
It does the closure-capturing of n for you.

Re: Common Python Mistakes

#76
post #15

I've always thought that #1 is a sign of an incorrect operation altogether. If you want to always modify the passed parameter, it doesn't make sense to have a default. If you want to return a modified version of the input, you should make a copy immediately and then you don't get this problem. Doing both an in-place modification and returning a modified object at the same time is just wrong.

A slightly more realistic example:

    class Bag(object):
        def __init__(self, items=[]):
            self.items = items

        def add_item(self, item):
            # check the item is valid
            self.items.append(item)

    bag1 = Bag()
    bag1.add('an item')

    bag2 = Bag()
    print(bag2.items)

Re: Common Python Mistakes

#78

Here’s a thought experiment for you—think of “common mistakes in language X” as “design flaws in language X” or “ways in which language X is surprising” and what could have been done to mitigate that.

Whether you choose 0-based indexing or 1-based indexing, somebody is going to be confused about indexing sometime.

Re: Common Python Mistakes

#79
post #75

Earlier quoted context omitted.

Some of it seems to come from people cargo-culting their knowledge of anonymous and first class functions, so they end up believing that the only way to pass a function around is to construct it anonymously.

In the particular case of constructing several similar functions that do essentially the same thing, lambda is a rather natural choice. Being more explicit and less hacky can well be combined with staying true to functional style: from functools import partial mul = lambda x, y: x*y # could use int.__mul__, too multipliers = [partial(mul, n) for n in range(5)] It does the closure-capturing of n for you.

A function defined with def would work just the same there.

It would have a big ugly 'return' in it and be a few characters longer, but it would work the same, so I don't see what lambda brings to it.

Re: Common Python Mistakes

#80
post #71
post #53

Earlier quoted context omitted.

I never knew you could do that. You don't need the continuation though. >>> l = ["A", "c""d"] >>> l ['A', 'cd']

You don't need the continuation, but that's where you're most likely to make the mistake. It'll catch you when you leave off the comma from the last element of the list, then go back later and add another element to the end.

Sorry, I am not followed. Can you show it instead? Thanks!
Post reply on HN