Anti-Patterns in Python Programming
lignos.org
Anti-Patterns in Python Programming
1–10 of 242 posts
Re: Anti-Patterns in Python Programming
#21. 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
#3- Bare except: statements (that catches everything, even Ctrl-C)
- Mutables as default function/method arguments
- Wildcard imports!
Re: Anti-Patterns in Python Programming
#4Great talk on avoiding some of the common pitfalls new python developers step in. Exposes some nice language features.
Re: Anti-Patterns in Python Programming
#5lyrics_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 thislyrics_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
#6The 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!
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# 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…
Re: Anti-Patterns in Python Programming
#8The 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!
It would really make sense to change the semantics of Python to fix this issue.
Re: Anti-Patterns in Python Programming
#9The 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!
Re: Anti-Patterns in Python Programming
#10I 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.