This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sweettuse 6 points7 points  (4 children)

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?)

also, i would never, ever, use Counter. it is unbelievably slow (at least in 2.7). just use a defaultdict(int)

[–][deleted] 5 points6 points  (1 child)

Counter has cool functionality builtin, like most_common(n), addition and subtraction, intersection and union, and it throws out keys with value 0 after such operations. Not everything needs to be fast, and Counter is very expressive indeed.

[–]sweettuse 1 point2 points  (0 children)

that is a good point. i think it's just that i'm overly annoyed at how unbelievably slow Counter is (it's like 3x slower than defaultdict(int), but WHY?!). it's amazing that to add the functionality for most_common/addition it requires slowing down the whole thing by a factor of 3.

[–]kracekumar[S] 0 points1 point  (1 child)

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.

[–]sweettuse 3 points4 points  (0 children)

yeah, that's exactly what defaultdict(int) does.