Numeric literals in other than base 10 - was Annoying octal notation

Erik Max Francis max at alcyone.com
Mon Aug 24 04:05:40 EDT 2009


MRAB wrote:
> James Harris wrote:
>> On 23 Aug, 00:16, Mel <mwil... at the-wire.com> wrote:
>>> James Harris wrote:
>>>> I have no idea why Ada which uses the # also apparently uses it to end
>>>> a number
>>>>   2#1011#, 8#7621#, 16#c26b#
>>> Interesting.  They do it because of this example from
>>> <http://archive.adaic.com/standards/83rat/html/ratl-02-01.html#2.1>:
>>
>> Thanks for providing an explanation.
>>
>>> 2#1#E8                    -- an integer literal of value 256
>>>
>>> where the E prefixes a power-of-2 exponent, and can't be taken as a 
>>> digit of
>>> the radix.  That is to say
>>>
>>> 16#1#E2
>>>
>>> would also equal 256, since it's 1*16**2 .
>>
>> Here's another suggested number literal format. First, keep the
>> familar 0x and 0b of C and others and to add 0t for octal. (T is the
>> third letter of octal as X is the third letter of hex.) The numbers
>> above would be
>>
>>   0b1011, 0t7621, 0xc26b
>>
>> Second, allow an arbitrary number base by putting base and number in
>> quotes after a zero as in
>>
>>   0"2:1011", 0"8:7621", 0"16:c26b"
>>
> Why not just put the base first, followed by the value in quotes:
> 
>     2"1011", 8"7621", 16"c26b"

It's always a bit impressive how syntax suggestions get more and more 
involved and, if you'll forgive me for saying, ridiculous as the 
conversation continues.  This is starting to get truly nutty.

What I've done in my projects is simply extend the pattern of 0x... for 
hexadecimal literals in C to 0b... for binary, 0o... for octal, 0d... 
for decimal (though redundant as that's the default), and so on.  (Go 
crazy and add 0t... for trinary and 0q... for quaternary if you feel 
like it.)  To me this always seemed elegant, simple, and understandable.

If arbitrary radix values is what's desirable, then some syntax like

	<radix>r<value>

(e.g., 8r024222570 for an octal number which represents a very lame 
joke) would work, but seems to me like huge overkill.  A normal string 
literal coupled with a "constructor" type function would seem far more 
appropriate -- and we already have that with `int`.

As for large literals, I'd go with having spaces indicate automatic 
concatenation (though only the first in the series can indicate the 
radix, whichever method you choose above).  It's the same as for 
strings, and it's the common SI recommendation for thousands separators 
anyway.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
   The little I know, I owe to my ignorance.
    -- Sacha Guitry



More information about the Python-list mailing list