471,337 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

string formatter %x and a class instance with __int__ or __long__cannot handle long

Hi

I'm using Python 2.4.4 on 32bit x86 Linux. I have a problem with printing
hex string for a value larger than 0x800000000 when the value is given to
% operator via an instance of a class with __int__(). If I pass a long value
to % operator it works just fine.

Example1 -- pass a long value directly. this works.
>>x=0x80000000
x
2147483648L
>>type(x)
<type 'long'>
>>"%08x" % x
'80000000'

Example2 -- pass an instance of a class with __int__()
>>class X:
.... def __init__(self, v):
.... self.v = v
.... def __int__(self):
.... return self.v
....
>>y = X(0x80000000)
"%08x" % y
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: int argument required
>>>
The behavior looks inconsistent. By the way __int__ actually
returned a long type value in the Example2. The "%08x" allows
either int or long in the Example1, however it accepts int only
in the Example2. Is this a bug or expected?

by the way same thing happends on a 64bit system with a
value of 0x8000000000000000.

Regards,
Kenji Noguchi
Jun 21 '07 #1
1 1013
On Jun 20, 8:24 pm, "Kenji Noguchi" <tokyo...@gmail.comwrote:
Hi

I'm using Python 2.4.4 on 32bit x86 Linux. I have a problem with printing
hex string for a value larger than 0x800000000 when the value is given to
% operator via an instance of a class with __int__(). If I pass a long value
to % operator it works just fine.

Example1 -- pass a long value directly. this works.>>x=0x80000000
>x
2147483648L
>type(x)
<type 'long'>
>"%08x" % x

'80000000'

Example2 -- pass an instance of a class with __int__()>>class X:

... def __init__(self, v):
... self.v = v
... def __int__(self):
... return self.v
...>>y = X(0x80000000)
>"%08x" % y

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: int argument required

The behavior looks inconsistent. By the way __int__ actually
returned a long type value in the Example2. The "%08x" allows
either int or long in the Example1, however it accepts int only
in the Example2. Is this a bug or expected?

by the way same thing happends on a 64bit system with a
value of 0x8000000000000000.

Regards,
Kenji Noguchi
In your second example y is an instance of class X...not an int. y.v
is an int. Are you hoping it will cast it to an int as needed using
your method? If so, I think you need to do so explicitly...ie "%08x"
% int(y)

~Sean

Jun 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Pierre Fortin | last post: by
32 posts views Thread by tshad | last post: by
5 posts views Thread by Alisa | last post: by
8 posts views Thread by Frank Rizzo | last post: by
21 posts views Thread by phpCodeHead | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.