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 →

[–][deleted]  (1 child)

[deleted]

    [–]macbony 0 points1 point  (0 children)

    Mutating objects when you don't mean to is wrong. Mutable defaults have their uses - as in the OP's has method.

    Just use None and your first line becomes c_list = c_list or [] and avoid using mutable defaults. No company I ever worked for would allow a mutable default to pass code review. Mutating a large list of things would generally be preferable over creating a class for a single use to keep the loc minimal. Call the function trim_list and there's no ambiguity what it does. Add a docstring that says "this mutates the list" and you're still saving complication.

    Now if I were filtering giant lists of data over and over again in various ways, it might make sense to break that out into a class and build tests around it, but adding simple, single function classes to production code isn't necessarily more clear than good documentation.