Re: Interesting list Validity (True/False)



On May 11, 2:28 pm, nufuh...@xxxxxxxxx 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>



I hope this helps (I have tried to post this twice already but it
seems to be going somewhere else) you help me.

What I would like to happen is:
else:
arg = sys.argv[1:]
print 'print arg', arg
print 'type(arg):', type(arg)
print 'arg is True?', arg == True
if arg != True:
print " No Option Provided"
print " help.py version", _ver_, "Copyright RDEG (c) 2007"
print " ", arg, "is an unrecognized option."
print " Progam Exit (0)"
sys.exit(0)

But as you can see by my output ['-o'] seems to be False as well as []
so the if happens regardless.

According to the "Book", ['-o'] should return True which should fail
the if, no?

.



Relevant Pages