473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 1061
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Pierre Fortin | last post by:
Hi! "Python Essential Reference" - 2nd Ed, on P. 47 states that a string format can include "*" for a field width (no restrictions noted); yet... >>> "%*d" % (6,2) # works as expected ' ...
3
by: Chrigel | last post by:
Hi all, We have problems deseralizing objects previously serialized as XML. This did work fine with .NET 1.1 but since we have installed SP1, deserializing fails (but serializing works). The...
4
by: Andreas Huber | last post by:
Hello there I need to serialize/deserialize some pretty simple data structures (no inheritance, few has relationships, ~20 classes) in three formats. One is XML (structure is not important as...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
5
by: Alisa | last post by:
How can i get the seralization of an object as string? Can i avoid saving a temporary file? thanks
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
8
by: Frank Rizzo | last post by:
How do I serialize Font object into a string that I can store in either INI file or the registry (I know it’s a bad thing, but need to do it nevertheless). Then, also, how do I deserialize it from...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.