For review: PEP 308 - If-then-else expression

Andrew Dalke adalke at mindspring.com
Mon Feb 10 04:48:22 EST 2003


Paul Paterson:
> It's possible that they might replace some instances of the initialization
idiom,
>
> def f(self, x=None):
>     self.x = x if x or []   # explicit is better
>
> in which case we would see more of them.

Actually, that can be done right now as

    self.x = x or []

I happen not to like it for some cases since

q = []
obj.f(q)
# Ah, ha! I still have a reference to the list through q!
assert obj.x is q

will raise an AssertionError.

I prefer

  if x is None:  x = []
  self.x = x

which would be written as

  self.x = x if x is not None else []

with this if/else expression.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list