Python style questions

D-Man dsh8290 at rit.edu
Fri Mar 16 11:18:57 EST 2001


On Fri, Mar 16, 2001 at 10:25:21AM -0500, Cary O'Brien wrote:
| I have a couple of style questons regarding larger Python programs.
| I'd be interested in comments.
|
| 4. I need to change things like "88aa99bb" (hex string) into
| integers.  Should I
| 
|    a. use expr("0x"+s) 
|    b. not use such things
|    c. ???

>>> i = int( "0xFF" , 16 )
>>> type( i )
<type 'int'>
>>> print "%d , %x" % ( i , i )
255 , ff
>>>

>>> i = int(  "88aa99bb" , 16 )
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: int() literal too large: 88aa99bb
>>>

>>> l = long( "88aa99bb" , 16 )
>>> type( l )
<type 'long int'>
>>> print "%d , %x" % ( l , l )
2292881851 , 88aa99bb
>>>


Curiosly enough, print's format string is supposed to be identical to
C's printf format string.  However the man page for printf says the %x
prints 0x then the number using A-F for 10-15.  Python only prints the
number leaving the 0x up to the user to print.  (On Solaris 8 and
Win2K, jython on Win2K with JDK 1.1.8, 1.2.2, 1.3.0)

-D





More information about the Python-list mailing list