Python print and types selection
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Mar 27 18:56:06 EDT 2009
En Fri, 27 Mar 2009 18:43:16 -0300, <mark.seagoe at gmail.com> escribió:
> Python 2.5, PC.
>
> I have a question about getting Python print to be able to recognize a
> long type.
>
> [...]
>
> print 'TEST 3'
> bird = bignumber(dog)
> print 'type(bird) = %s' % type(bird)
> print 'bird val = 0x%016X' % long(bird)
> print 'bird val = 0x%016X' % bird
>
> When I run this, I get:
> [...]
> TEST 3
> type(bird) = <class '__main__.bignumber'>
> bignumber.__long__ returning a <type 'long'>
> bird val = 0x123456789ABCDEF0
> bignumber.__int__ returning a <type 'long'>
> Traceback (most recent call last):
> File "C:\PythonTesting\bignmber.py, line 32, in <module>
> print 'bird val = 0x%016X' % bird
> TypeError: int argument required
>
> Python print recognizes the local constant "dog", but it goes and
> fetches the __int__ type from my object-based class, even though it's
> value is a long. Then Python print doesn't expect a long to come back
> and bombs out. I don't want to force the main program to cast the
> long value getting returned. I know Python print can print a long,
> but how can I make it select the __long__ instead of the __int__ ?
This bug was corrected in version 2.6 - see
http://bugs.python.org/issue1742669
If you have to stay with 2.5 I'm afraid any solution would require to
modify your code:
-- put long() around those arguments
-- "%s" % format_hex(val) (where format_hex does the long conversion)
-- redefine __str__ and use %s
--
Gabriel Genellina
More information about the Python-list
mailing list