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

Paul Rubin phr-n2003b at NOSPAMnightsong.com
Sat Feb 8 21:42:31 CET 2003


Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:
> >An example:
> >
> >    def get_arg(self, default=1):
> >        """Return any prefix argument that the user has supplied,
> >        returning `default' if there is None.  `default' defaults
> >        (groan) to 1."""
> >        if self.arg is None:
> >            return default
> >        else:
> >            return self.arg
> 
> Yuck! An pointless function that boils down to:
> 
> self.arg or default
> 
> in current parlance.

Actually wrong.  

  self.arg or default

will incorrectly give the default, if self.arg is 0 or the empty
string or an empty dict, etc.  A conditional expression is really the
right thing here.




More information about the Python-list mailing list