Live data from Hacker News

Anti-Patterns in Python Programming

lignos.org

21–30 of 242 posts

Re: Anti-Patterns in Python Programming

#21
post #10

> write a list comprehension (...) code just looks a lot cleaner and what you're doing is clearer. I know how to use list comprehensions, but often avoid using them and use the standard for loops. List comprehensions look nice and clean for small examples, but they can easily get long and become mentally hard to parse. I would rather go for three 30 character lines instead of one 90 character line.

Depends on how you want to program: imperative vs functional.

Personally I think list comprehensions are the most beautiful part of Python, though sometimes I use map() when I'm trying to be explicitly functional (I realize it's allegedly slower, etc).

Generally I think list comprehensions are cleaner and allow you to write purer functions with fewer mutable variables. I disagree that deeply nested for loops are necessarily more readable.

Re: Anti-Patterns in Python Programming

#23
Instead of this:

    # Do this  
    lyrics_set = set(lyrics_list) # Linear time set construction  
    words = make_wordlist()  
    for word in words:  
        if word in lyrics_set: # Constant time  
            print word, "is in the lyrics"
You could do this:

    lyrics_set = set(lyrics_list)  
    words = set(make_wordlist())  
    matched_words = list(lyrics_set & words)  
    for word in matched_words:  
        print word, "is in the lyrics"

Re: Anti-Patterns in Python Programming

#24
post #6

Earlier quoted context omitted.

Possibly the most interesting anti-pattern I saw was: a_list_of_words = "my list of words".split(" ") I never enquired why, since there were bigger issues in the code e.g. "unit testing" by running the code, taking the result and putting it as the check value. By running repr(value), copying out the string then comparing self.assertEqual(repr(value), '[ , ...]')

Why even use a list here? Tuples are for immutable/constant data. a_tuple_of_words = ("my", "tuple", "of", "words") or a_tuple_of_words = "my", "tuple", "of", "words"

...because it's a list? Tuples were supposed to have a structure (at least that's what all the rest of the world thinks of them), so iterating through combination of apples, cars and languages makes no sense whatsoever.

But yes, Python misses entirely the point of tuples, treating them as read-only lists.

http://dozzie.jogger.pl/2014/04/11/python-tuples-the-useless...

Re: Anti-Patterns in Python Programming

#26
post #6

Earlier quoted context omitted.

Possibly the most interesting anti-pattern I saw was: a_list_of_words = "my list of words".split(" ") I never enquired why, since there were bigger issues in the code e.g. "unit testing" by running the code, taking the result and putting it as the check value. By running repr(value), copying out the string then comparing self.assertEqual(repr(value), '[ , ...]')

perhaps that line was written by someone used to Perl, where they would have had @a_list_of_words = qw/my list of words/; there

Or rubyst:

  a_list_of_words = %w{my list of words}

Re: Anti-Patterns in Python Programming

#27

read this right after using a range in a loop in order to get the index. Can't believe I went this long without knowing about enumerate.

It first mentions enumerate as a footnote after looping with a range:

https://docs.python.org/2/tutorial/controlflow.html#for-stat...

But the tutorial in the Python docs has pretty high information density and good coverage of things like this.

(I think there is some risk that this comment will be interpreted as If you don't know enumerate you need to look at the tutorial. That isn't what I intend, I just want to point out that the tutorial is a reasonably dense resource that hits on a lot of stuff like enumerate.)

Re: Anti-Patterns in Python Programming

#28
post #23

Instead of this: # Do this lyrics_set = set(lyrics_list) # Linear time set construction words = make_wordlist() for word in words: if word in lyrics_set: # Constant time print word, "is in the lyrics" You could do this: lyrics_set = set(lyrics_list) words = set(make_wordlist()) matched_words = list(lyrics_set & words) for word in matched_words: print word, "is in the lyrics"

Of, off the top of my head:

    for word in (set(lyrics_list) & set(words)):
        print('{} is in the lyrics'.format(word))

Re: Anti-Patterns in Python Programming

#29
post #4

Check out Raymond Hettingers Transforming Code into Beautiful, Idiomatic Python talk on youtube [1]. Great talk on avoiding some of the common pitfalls new python developers step in. Exposes some nice language features. [1]: https://www.youtube.com/watch?v=OSGv2VnC0go

And the slides are here:

https://speakerdeck.com/pyconslides/transforming-code-into-b...

Re: Anti-Patterns in Python Programming

#30

Earlier quoted context omitted.

I do that all the time in the interpreter, especially when slicing pandas DataFrame objects, e.g.: df_subset = df['date buyer nwidgets'.split()] That is far easier to type than the explicit list, with all its punctuation. Now, it's definitely weird that they did a `split(" ")` rather than just using the default, but the idea is the same. I do try to strip stuff like that out before I put it into a script, replacing i…

It's not weird, it's wrong: In [1]: "a string r".split(" ") Out[1]: ['a', '', '', '', 'string', '', '', '', '', 'r'] In [2]: "a string r".split() Out[2]: ['a', 'string', 'r']

In their case though that wouldn't have been a problem as each word was split on a single space.
Post reply on HN