Conditional operator in Python?

Steven D. Majewski sdm7g at Virginia.EDU
Wed Apr 4 14:20:25 EDT 2001


On Wed, 4 Apr 2001, Alex Martelli wrote:

> 
> I suspect spaces (or underscores?) within _numeric literals_ might
> prove more popular -- I'd LOVE to be able to write, e.g.:
>     a = 1 000 000 000
> to make the number "one billion" more legible -- but maybe it's not
> as good an idea as it seems at first glance: if the number of digits
> between separating spaces was not checked, it might be misleading;
> if it WAS checked, then I'm not sure that "three by three" is OK in
> all locales (but maybe non-US locales need not be supported here,
> just as they aren't, for example, for decimal-point).  [Commas as
> digit-separators in literals are not acceptable because of backwards
> compatibility -- "a=1,000" already has meaning, binding a to a tuple
> of 2 elements, of course!].


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

-- Steve 






More information about the Python-list mailing list