Inline Conditionals?
Hobbs, Michael
Michael_Hobbs at archway.com
Tue Aug 24 19:51:43 EDT 2004
Adonis <adonisv at DELETETHISTEXTearthlink.net> wrote...
> "Joshua Ginsberg" <joshg at brainstorminternet.net> wrote in message
> news:mailman.2307.1093385983.5135.python-list at python.org...
> > Is there any plan to include inline conditionals in Python?
> For example:
> >
> > def isNegative(x):
> > return x < 0 ? True : False
> >
> > Thanks!
> >
> > -jag
> >
> > --
> > Joshua Ginsberg <joshg at brainstorminternet.net>
> > Brainstorm Internet Network Operations
> >
>
> How about something like:
>
> >>> def iif(condition, true=True, false=False):
> ... if condition:
> ... return true
> ... return false
> ...
> >>> iif('foo' == 'bar', 'w00t', 'l33t')
> 'l33t'
> >>> iif('bar' == 'bar', 'w00t', 'l33t')
> 'w00t'
> >>> iif('bar' == 'bar')
> True
> >>> iif('foo' == 'bar')
> False
> >>>
Probably good enough, but not an exact replacement. For example, such a
function wouldn't
work well for this:
>>> name = line is None ? None : line[4:]
If you call iif() when line is None, you get an error:
>>> name = iif(line is None, None, line[4:])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
Because `line[4:]' is evaluated regardless if `line is None'.
More information about the Python-list
mailing list