Question from a python newbie

J. Clifford Dyer jcd at sdf.lonestar.org
Thu Dec 13 11:11:35 EST 2007


On Thu, Dec 13, 2007 at 04:57:04PM +0100, Remco Gerlich wrote regarding Re: Question from a python newbie:
> 
>    On Dec 13, 2007 4:39 PM, Russell <[1]rcooknet at gmail.com> wrote:
> 
>      I've been learning Python slowly for a few months, coming from a
>      C/C+
>      +, C#, Java, PHP background.  I ran across a code fragment I'm
>      having
>      trouble wrapping my brain around.  I've searched the Language
>      Reference and was not able to find any info regarding the structure
>      of
>      this code fragment:
>      int(text) if text.isdigit() else text
> 
>    Hi,
>    It's a pretty new construct; basically it's Python's version of the ? :
>    "ternary operator" in other languages.
>    The line above is equivalent to
>    text.isdigit() ? int(text) : text
>    in, for instance, C.
>    Remco Gerlich
> 

Or, if you don't know C.  It is equivalent to

if text.isdigit():
	int(text)
else:
	text



More information about the Python-list mailing list