Re: Interesting list Validity (True/False)



On May 11, 3:36 pm, nufuh...@xxxxxxxxx wrote:
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?

You're mistaking the porperties of an object for the object itself.

if arg:

tests the property (of being empty).

if arg==True:

tests the type property (whether a list is a boolean).

Change the code I gave above to be:

print
if arg:
print 'The argument given was:',arg
else:
print 'No argument given'
print

then you'll get

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

.



Relevant Pages