Live data from Hacker News

Anti-Patterns in Python Programming

lignos.org

1–10 of 242 posts

Re: Anti-Patterns in Python Programming

#2
Interesting read, a couple of things I noticed though:

1. In "Checking for contents in linear time" both examples are the same. Perhaps remove the list entirely in the second example

2. Itertools.islice helps if you need to slice a list with a bajillion elements

Re: Anti-Patterns in Python Programming

#5
# Avoid this

lyrics_list = ['her', 'name', 'is', 'rio']

words = make_wordlist() # Pretend this returns many words that we want to test

for word in words:

    if word in lyrics_list: # Linear time

        print word, "is in the lyrics"
# Do this

lyrics_list = ['her', 'name', 'is', 'rio']

lyrics_set = set(lyrics_list) # Linear time set construction

words = make_wordlist() # Pretend this returns many words that we want to test

for word in words:

    if word in lyrics_list: # Constant time

        print word, "is in the lyrics"
the second example should read ... if word in lyrics_set: ...

Re: Anti-Patterns in Python Programming

#6
post #3

The more frequent and dangerous pitfalls are, in my humble opinion: - Bare except: statements (that catches everything , even Ctrl-C) - Mutables as default function/method arguments - Wildcard imports!

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), '[, ...]')

Re: Anti-Patterns in Python Programming

#7
post #5

# Avoid this lyrics_list = ['her', 'name', 'is', 'rio'] words = make_wordlist() # Pretend this returns many words that we want to test for word in words: if word in lyrics_list: # Linear time print word, "is in the lyrics" # Do this lyrics_list = ['her', 'name', 'is', 'rio'] lyrics_set = set(lyrics_list) # Linear time set construction words = make_wordlist() # Pretend this returns many words that we want to test for…

Just to point out, if you really will have a tiny list and that's knowable, it's possible this example would be best with a straight linear time check. It could be fewer operations than hashing a string and looking it up. Practically pedantry though.

Re: Anti-Patterns in Python Programming

#8
post #3

The more frequent and dangerous pitfalls are, in my humble opinion: - Bare except: statements (that catches everything , even Ctrl-C) - Mutables as default function/method arguments - Wildcard imports!

> Mutables as default function/method arguments

It would really make sense to change the semantics of Python to fix this issue.

Re: Anti-Patterns in Python Programming

#9
post #3

The more frequent and dangerous pitfalls are, in my humble opinion: - Bare except: statements (that catches everything , even Ctrl-C) - Mutables as default function/method arguments - Wildcard imports!

Also, not knowing what scope a variable has when creating closures.

Re: Anti-Patterns in Python Programming

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

Post reply on HN