MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/28utww/writing_better_python_code/cif3ehw/?context=3
r/Python • u/kracekumar • Jun 23 '14
126 comments sorted by
View all comments
7
def bar(names=None): if not names: names = [] names.append('blah')
this is wrong. it should be if names is None:. (what happens if you pass in an empty list that you want to append to?)
if names is None:
also, i would never, ever, use Counter. it is unbelievably slow (at least in 2.7). just use a defaultdict(int)
Counter
defaultdict(int)
1 u/kracekumar Jun 23 '14 Counter has its own benefit. If the key doesn't exist it can return 0. There are lot of scenarios where this is super useful. 4 u/sweettuse Jun 23 '14 yeah, that's exactly what defaultdict(int) does.
1
Counter has its own benefit. If the key doesn't exist it can return 0. There are lot of scenarios where this is super useful.
4 u/sweettuse Jun 23 '14 yeah, that's exactly what defaultdict(int) does.
4
yeah, that's exactly what defaultdict(int) does.
7
u/sweettuse Jun 23 '14
this is wrong. it should be
if names is None:. (what happens if you pass in an empty list that you want to append to?)also, i would never, ever, use
Counter. it is unbelievably slow (at least in 2.7). just use adefaultdict(int)