Generally speaking, one should just return from within the loop.
Anti-Patterns in Python Programming
31–40 of 242 posts
Re: Anti-Patterns in Python Programming
#32Instead 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))
print " is in the lyrics \n".join([set(lyrics_list) & set(words)]), "is in the lyrics"Re: Anti-Patterns in Python Programming
#33Re: Anti-Patterns in Python Programming
#34The 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!
Given a function like:
def append_one(l=[]):
l.append(1)
return l
What does this return each time? >>> append_one()
>>> append_one()
>>> append_one()Re: Anti-Patterns in Python Programming
#35 alist = [foo(word) for word in words]
is considered more Pythonic than map alist = map(foo, words)Re: Anti-Patterns in Python Programming
#36I always struggle to understand why a list comprehension alist = [foo(word) for word in words] is considered more Pythonic than map alist = map(foo, words)
Where as map could be anything. It could be redefined for all you'd know.
Re: Anti-Patterns in Python Programming
#37I always struggle to understand why a list comprehension alist = [foo(word) for word in words] is considered more Pythonic than map alist = map(foo, words)
alist = [foo(word) for word in words if word.startswith('a')]
alist = map(foo, filter(lambda word: word.startswith('a'), words))
Which reads better?Re: Anti-Patterns in Python Programming
#38Earlier quoted context omitted.
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...
Lighter-weight, immutable collections have a use case. The code in OP appears to be one where it makes sense. I follow the rule where variables are mutable IFF they need to be mutable.
Re: Anti-Patterns in Python Programming
#39Earlier 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), '[ , ...]')
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…
(That sentence I wrote about using hashable types need not apply, sorry!)
If you ran that code, you would get this error:
TypeError: list indices must be integers, not listRe: Anti-Patterns in Python Programming
#40Earlier 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"