Understanding (and getting rid) of optparse.py:668: FutureWarning: %u/%o/%x/%X of negative int will return a

Peter Otten __peter__ at web.de
Fri Sep 26 18:21:41 CEST 2008

hofer wrote:

> Hi,
> I get following warning with a python script:
> optparse.py:668: FutureWarning: %u/%o/%x/%X of negative int will
> return a signed string in Python 2.4 and up
> my code:
> from optparse import OptionParser
> if __name__ == '__main__':
>     parser = OptionParser()
>     parser.add_option('-G','--green',action= 'store_const', const=
> '#00FF00' ,  dest='color',
>                        default='#808080',
>                     help='life is so green')
>     parser.add_option('-R','--red',action= 'store_const', const =
> '#FF0000' , dest='color',
>                     help='I just see red')
>     # add more elaborated command line parsing and help text here
>     (options,argv) = parser.parse_args()
>     print 'options',options
> I assume python wants to tell me that newer version will behave
> differently for numeric arguments
> What I wonder is: Why do I get the warning if my code doesn't try to
> parse any numbers?

The culprit is

print options

If you look into optparse.py you'll see that part of the __repr__() method
of the Value class is the object's address, roughly

"%x" % id(self)

id(self) is just the Value instance's address in memory which seems to be 
>= 0x80000000 (assuming you are on a 32-bit machine) in your case.

Such numbers were interpreted as negative ints but are now treated as
positive longs. Read


for details.

> Is there any way to get rid of the warning without having to change
> the python version?
> (I noticed, the warning disappears if I remove the line printing
> options)

You can print options.__dict__ instead of options with little loss of
information, or turn the warning off

python -Wignore::FutureWarning myscript.py


More information about the Python-list mailing list