Conditional operator in Python?

Tim Peters tim.one at home.com
Wed Apr 4 15:05:34 EDT 2001


[Steven D. Majewski]
> Obviously a performance hit, but if you want readability:
>
> >>> def Number( *args ):
> ...     t = 0
> ...     for x in args:
> ...             t = t * 1000L
> ...             assert 0 <= x < 1000, 'Out of range' 
> ...             t = t + x
> ...     return t
> ... 
> >>> Number( 1,000,000 )
> 1000000
> >>> Number( 1,234,567)
> 1234567
> >>> Number( 1,200,300,400,500 )
> 1200300400500L
> >>> Number( 1,200,300,400,500.25 )
> 1200300400500.25
>
> Of course, you can also have confusing things like:
>
> >>> Number( 1,2,3 )
> 1002003

That's not confusing.  This is <wink>:

>>> Number(1,064)
1052L
>>>

hard-to-believe-that-semantically-significant-leading-zeroes-
    ever-seemed-like-"a-good-idea"-to-anyone-ly y'rs  - tim





More information about the Python-list mailing list