Interesting list Validity (True/False)

mensanator at aol.com mensanator at aol.com
Fri May 11 16:09:50 EDT 2007


On May 11, 2:28 pm, nufuh... at gmail.com wrote:
> Hello all,
>
> First let me appologise if this has been answered but I could not find
> an acurate answer to this interesting problem.
>
> If the following is true:
>     C:\Python25\rg.py>python
>     Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32
> bit (Intel)] on
>     win32
>     Type "help", "copyright", "credits" or "license" for more
> information.
>     >>> [] == []
>     True
>     >>> ['-o'] == []
>     False
>     >>> ['-o'] == False
>     False
>     >>>
>
> Then why do I get the following results:
>     C:\Python25\rg.py>help.py -o
>     print arg ['-o']
>     type(arg): <type 'list'>
>     arg is True? False
>         help.py version 1.0 Copyright RDEG (c) 2007
>         ['-o'] is an unrecognized option.
>         Progam Exit (0)
>
> <python>
>     import sys
>
>     _ver_ = 1.00
>
>     if '-h' in sys.argv or '--help' in sys.argv:
>         print
>         print "    help.py Version", _ver_, "Copyright RDEG (c) 2007"
>         print '''
>
>         Options : -h, --help -- display this message
>             Progam Exit (0)'''
>         sys.exit(0)
>     else:
>         arg = sys.argv[1:]
>         print 'print arg', arg
>         print 'type(arg):', type(arg)
>         print 'arg is True?', arg == True
>         print "    help.py version", _ver_, "Copyright RDEG (c) 2007"
>         print "    ", arg, "is an unrecognized option."
>         print "    Progam Exit (0)"
>         sys.exit(0)
> </python>


Does this clear things up?


import sys


_ver_ = 1.00


if '-h' in sys.argv or '--help' in sys.argv:
    print
    print "    help.py Version", _ver_, "Copyright RDEG (c) 2007"
    print '''


    Options : -h, --help -- display this message
        Progam Exit (0)'''
    sys.exit(0)
else:
    arg = sys.argv[1:]
    print 'print arg', arg
    print 'type(arg):', type(arg)
    print 'arg is True?', arg == True

    print
    if arg:
        print 'was True'
    else:
        print 'was False'
    print

    print "    help.py version", _ver_, "Copyright RDEG (c) 2007"
    print "    ", arg, "is an unrecognized option."
    print "    Progam Exit (0)"
    sys.exit(0)

##    C:\python25\user>python arghhh!.py -o
##    print arg ['-o']
##    type(arg): <type 'list'>
##    arg is True? False
##
##    was True
##
##        help.py version 1.0 Copyright RDEG (c) 2007
##         ['-o'] is an unrecognized option.
##        Progam Exit (0)

##    C:\python25\user>python arghhh!.py
##    print arg []
##    type(arg): <type 'list'>
##    arg is True? False
##
##    was False
##
##        help.py version 1.0 Copyright RDEG (c) 2007
##         [] is an unrecognized option.
##        Progam Exit (0)




More information about the Python-list mailing list