strange test for None

skip at pobox.com skip at pobox.com
Sat Feb 3 09:08:57 EST 2007


>>>>> "karoly" == karoly kiripolszky <karoly.kiripolszky> writes:

    karoly> in my server i use the following piece of code:
    karoly>             ims = self.headers["if-modified-since"]
    karoly>             if ims != None:
    karoly>                 t = int(ims)

    karoly> and i'm always getting the following error:

    karoly>     t = int(ims)
    karoly> ValueError: invalid literal for int(): None

Try printing repr(ims), type(ims) and id(ims) then compare that with
repr(None), type(None) and id(None).   My guess is you have a string whose
value is "None":

    % python2.4
    Python 2.4.1 (#3, Jul 28 2005, 22:08:40) 
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1671)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> int("None")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: invalid literal for int(): None

The output in 2.5 makes it more clear what you've got:

    % python2.5
    Python 2.5c1 (release25-maint:51339, Aug 17 2006, 22:15:14) 
    [GCC 4.0.0 (Apple Computer, Inc. build 5026)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    in>>> int("None")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: 'None'

Skip



More information about the Python-list mailing list