[Tutor] Unsigned numerical literals?

Gregor Lingl glingl@aon.at
Thu, 20 Dec 2001 01:35:18 +0100


----- Original Message -----
From: "Lee-Shanok, Bruce" <Bruce.Lee-Shanok@cognos.com>
To: <tutor@python.org>
Sent: Wednesday, December 19, 2001 9:54 PM
Subject: [Tutor] Unsigned numerical literals?


> Hello all. Quick question: I'm writing a wrapper for some C calls in
Python.
> The issue here is that the C code takes an unsigned long. Now, Python
> integers are, to the best of my understanding, also 32 bit, but not
unsigned
> by default. Is there a clean way to force them to be unsigned? I'd hate to
> have to force the users of the Python code to write an "L" at the end of
> every value they bring in....
>

This would only make sense if you used the input-statement:

If you tried:

>>> a = input("any integer: ")
any integer: 123412312341234
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in ?
    a = input("any integer: ")
OverflowError: integer literal too large
>>>

To avoid this, use raw_input:

>>> a = long(raw_input("any integer: "))
any integer: 123412341234
>>> a
123412341234L
>>>

thus reading in a string and converting it immediately to a float.

Gregor

P.S.: Since the day after tomorrow you will get with Python V 2.2:

Python 2.2c1 (#27, Dec 14 2001, 13:15:16) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> a = input("any integer: ")
any integer: 123412312341234
>>> a
123412312341234L
>>>

Python V2.2 will do the conversion int=>long automatically

P.P.S.: Perhaps - depending on your application - it would be better
to use:

>>> try:
 a = long(raw_input("any integer: "))
except: # if something weird was input
 print "Nonsense!"


any integer: 123412312341234xyz
Nonsense!
>>>

... to catch unplausible userinput.


> Cheers,
> Bruce
>