473,224 Members | 1,590 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,224 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 1059
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 its 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()
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.