Peter Hansen wrote: > def meth(self, things=None): > self.things = things or [] > [snip] > > The alternative is fine too, but insisting on it would be pedantic, and > if you have more than one of these it is definitely less readable (and, > therefore, not Pythonic): > > def meth(self, things=None): > if things: > self.things = things > else: > self.things = []
Probably worth pointing out that there is at least one more alternative: def meth(self, things=None): if things is None: things = [] self.things = things I usually opt for this one, mainly because "things or []" makes me nervous -- it has different behavior if the user passes in an empty list: py> class Things1(object): ... def __init__(self, things=None): ... if things is None: ... things = [] ... self.things = things ... py> class Things2(object): ... def __init__(self, things=None): ... self.things = things or [] ... py> lst = [] py> thing = Things1(lst) py> thing.things.append(100) py> thing.things, lst ([100], [100]) py> lst = [] py> thing = Things2(lst) py> thing.things.append(100) py> thing.things, lst ([100], []) That said, I do use "and" and "or" occasionally when I'm sure I don't have to worry about complications like the above. I've probably even used them in an assignment statement. ;) STeVe -- http://mail.python.org/mailman/listinfo/python-list