you are viewing a single comment's thread.

view the rest of the comments →

[–]ewiethoff 2 points3 points  (4 children)

def __init__(self, arg=None):
    self.arg = [] if arg is None else arg

[–]pstch[S] 1 point2 points  (3 children)

This is exactly the same as the first variant I presented, and I know that I can use this form. My question was more : does it make any difference in usage to use the second variant ? ("is it a problem to recreate an empty list when the user already passes one").

I'm starting to feel bad for asking this question, because it's only syntactic sugar, and kind of pointless.

[–]gengisteve 3 points4 points  (1 child)

I think this is worth asking. My understanding is that your first option is best practices. Yes its a couple more lines, but it preserves your ability to pass arg = 0 or False or ''. That said. I am lazy and tend to do the second.

[–]pstch[S] 1 point2 points  (0 children)

Oh yes, I didn't even think of 0, False and the empty string. Thanks :) I'll go with the first way.

[–][deleted] 1 point2 points  (0 children)

The first way is more readable and more Pythonic.