Interesting list Validity (True/False)

N

nufuhsus

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>
 
G

Grant Edwards

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)

You got those results because that's what your program does.

Were you intending it to do something else? If so, you're
going to have to explain what you wanted, because we can't read
your mind.
 
M

mensanator

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)
 
G

Grant Edwards

According to my output, it seems that arg is False even when I
give an option of '-o' which according to the book should be
True. No?

'-o' is not equal to True. However, that does not mean it
evaluates to false when tested by an if or while statement.
If arg == ['-o'] then shouldn't arg == True return True and
skip the if?

No. See the folloing link regarding the "truth value" of an
object:

http://docs.python.org/lib/truth.html

There are many objects other than True that evaluate to "true"
in the context of an if/while statement. Just because an
objecty has a "true" truth-value doesn't mean that it is equal
to the True object.
 
N

nufuhsus

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?
 
C

Carsten Haese

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

Your confusion stems from the fact that for a given object, the answer
to the following three questions can be vastly different:
a) Is the object identical to True?
b) Is the object equal to True?
c) Is the object considered to be True in an "if" statement?

Observe:
.... if obj is True: print repr(obj), "is identical to True."
.... else: print repr(obj), "is not identical to True."
.... if obj == True: print repr(obj), "is equal to True."
.... else: print repr(obj), "is not equal to True."
.... if obj: print repr(obj), "is considered to be True by if."
.... else: print repr(obj), "is not considered to be True by if."
....True is identical to True.
True is equal to True.
True is considered to be True by if.1 is not identical to True.
1 is equal to True.
1 is considered to be True by if.
[1] is not identical to True.
[1] is not equal to True.
[1] is considered to be True by if.
[] is not identical to True.
[] is not equal to True.
[] is not considered to be True by if.

Testing whether an object is equal to True is a much stronger test than
whether it is considered to be True in an 'if' statement, and the test
for identity is stronger still. Testing whether an object is equal to
True or identical to True is useless in most Python programs.

So, rather than doing this:

if thing==True:
# blah

Just do this:

if thing:
# blah

Hope this helps,
 
N

nufuhsus

According to my output, it seems that arg is False even when I
give an option of '-o' which according to the book should be
True. No?

'-o' is not equal to True. However, that does not mean it
evaluates to false when tested by an if or while statement.
If arg == ['-o'] then shouldn't arg == True return True and
skip the if?

No. See the folloing link regarding the "truth value" of an
object:

http://docs.python.org/lib/truth.html

There are many objects other than True that evaluate to "true"
in the context of an if/while statement. Just because an
objecty has a "true" truth-value doesn't mean that it is equal
to the True object.

--
Grant Edwards grante Yow! Why don't you ever
at enter any CONTESTS,
visi.com Marvin?? Don't you know
your own ZIPCODE?

OK. Then how would you differenciate between a call with an option
versus one without (e.g. help.py -o (where arg == ['-o']) Vs. help.py
(where arg == []))?
 
N

nufuhsus

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

Your confusion stems from the fact that for a given object, the answer
to the following three questions can be vastly different:
a) Is the object identical to True?
b) Is the object equal to True?
c) Is the object considered to be True in an "if" statement?

Observe:

... if obj is True: print repr(obj), "is identical to True."
... else: print repr(obj), "is not identical to True."
... if obj == True: print repr(obj), "is equal to True."
... else: print repr(obj), "is not equal to True."
... if obj: print repr(obj), "is considered to be True by if."
... else: print repr(obj), "is not considered to be True by if."
...>>> check_trueness(True)

True is identical to True.
True is equal to True.
True is considered to be True by if.>>> check_trueness(1)

1 is not identical to True.
1 is equal to True.
1 is considered to be True by if.>>> check_trueness([1])

[1] is not identical to True.
[1] is not equal to True.
[1] is considered to be True by if.>>> check_trueness([])

[] is not identical to True.
[] is not equal to True.
[] is not considered to be True by if.

Testing whether an object is equal to True is a much stronger test than
whether it is considered to be True in an 'if' statement, and the test
for identity is stronger still. Testing whether an object is equal to
True or identical to True is useless in most Python programs.

So, rather than doing this:

if thing==True:
# blah

Just do this:

if thing:
# blah

Hope this helps,

Thanks Carsten (& all), I will give the if thing: # blah trick. I
guess I am starting to seem my own confusion. As Grant mentioned, I
was comparing ['-o'] to True which of course is False :eek:)

However, how would you test for the falsness of the object arg?
 
C

Carsten Haese

OK. Then how would you differenciate between a call with an option
versus one without (e.g. help.py -o (where arg == ['-o']) Vs. help.py
(where arg == []))?

if arg:
print "With options"
else:
print "Without options"
 
N

nufuhsus

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
Your confusion stems from the fact that for a given object, the answer
to the following three questions can be vastly different:
a) Is the object identical to True?
b) Is the object equal to True?
c) Is the object considered to be True in an "if" statement?
... if obj is True: print repr(obj), "is identical to True."
... else: print repr(obj), "is not identical to True."
... if obj == True: print repr(obj), "is equal to True."
... else: print repr(obj), "is not equal to True."
... if obj: print repr(obj), "is considered to be True by if."
... else: print repr(obj), "is not considered to be True by if."
...>>> check_trueness(True)
True is identical to True.
True is equal to True.
True is considered to be True by if.>>> check_trueness(1)
1 is not identical to True.
1 is equal to True.
1 is considered to be True by if.>>> check_trueness([1])
[1] is not identical to True.
[1] is not equal to True.
[1] is considered to be True by if.>>> check_trueness([])
[] is not identical to True.
[] is not equal to True.
[] is not considered to be True by if.
Testing whether an object is equal to True is a much stronger test than
whether it is considered to be True in an 'if' statement, and the test
for identity is stronger still. Testing whether an object is equal to
True or identical to True is useless in most Python programs.
So, rather than doing this:
if thing==True:
# blah
Just do this:
if thing:
# blah
Hope this helps,
- Show quoted text -

Thanks Carsten (& all), I will give the if thing: # blah trick. I
guess I am starting to seem my own confusion. As Grant mentioned, I
was comparing ['-o'] to True which of course is False :eek:)

However, how would you test for the falsness of the object arg?- Hide quoted text -

- Show quoted text -

Would that be arg is not True: # blah.?
 
R

Rob Williscroft

wrote in in
comp.lang.python:
[] == [] True
['-o'] == [] False
['-o'] == False False

To test wether something is true use if.
To test wether something is false use if not.

The python values "True" and "False" are for when you need to
*store* a boolean value (for later testing).

I you want to to see if an arbitry expression would test as true
or false at the interactive prompt use bool():
bool([]) False
bool(['-o']) True

There is *never* any need to write things like:

expression == True

or:
expression == False

Once you stop doing this things will become much simpler.

Rob.
 
M

mensanator

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)
 
N

nufuhsus

Just an update of my output after Carsten and company's advice:

<out>
C:\Python25\rg.py>help.py -h

help.py Version 1.0 Copyright RDEG (c) 2007


Options : -h, --help -- display this message
Progam Exit (0)

C:\Python25\rg.py>help.py -i
print arg ['-i']
type(arg): <type 'list'>
arg is True? False
help.py version 1.0 Copyright RDEG (c) 2007
['-i'] is an unrecognized option.
Progam Exit (0)

C:\Python25\rg.py>help.py -i
help.py version 1.0 Copyright RDEG (c) 2007
['-i'] is an unrecognized option.
Progam Exit (0)

C:\Python25\rg.py>help.py
No Option provided
help.py version 1.0 Copyright RDEG (c) 2007
No Option is an unrecognized option.
Progam Exit (0)
</out>

Thanks again.
 
C

Carsten Haese

if arg==True:

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

That sounds nonsensical and incorrect. Please explain what you mean.

"if arg==True" tests whether the object known as arg is equal to the
object known as True.

Regards,
 
M

mensanator

That sounds nonsensical and incorrect. Please explain what you mean.

<quote>
Sec 2.2.3:
Objects of different types, except different numeric types and
different string types, never compare equal;
 
C

Carsten Haese

<quote>
Sec 2.2.3:
Objects of different types, except different numeric types and
different string types, never compare equal;
</quote>

That doesn't explain what you mean. How does "if arg==True" test whether
"a list is a boolean"?
 
M

mensanator

That doesn't explain what you mean. How does "if arg==True" test whether
"a list is a boolean"?
<type 'bool'>


Actually, it's this statement that's non-sensical.

<quote>
"if arg==True" tests whether the object known as arg is equal to the
object known as True.
</quote>

None of these four examples are "equal" to any other.
a = 1
b = (1,)
c = [1]
d = gmpy.mpz(1)

type(a)
a==b False
b==c False
a==d
True

And yet a==d returns True. So why doesn't b==c
also return True, they both have a 1 at index position 0?
 
S

Steven D'Aprano

I should point out that only applies to built-in types, not custom classes.

<type 'bool'>


That still doesn't make sense. However, using my incredible psychic
ability to read between the lines, I think what Mensanator is trying (but
failing) to say is that "if arg==True" first tests whether arg is of type
bool, and if it is not, it knows they can't be equal. That's not actually
correct. We can check this:
.... return arg == True
.... 2 0 LOAD_FAST 0 (arg)
3 LOAD_GLOBAL 0 (True)
6 COMPARE_OP 2 (==)
9 RETURN_VALUE


As you can see, there is no explicit type test. (There may or may not be
an _implicit_ type test, buried deep in the Python implementation of the
COMPARE_OP operation, but that is neither here nor there.)

Also, since bool is a subclass of int, we can do this:
True





Actually, it's this statement that's non-sensical.

<quote>
"if arg==True" tests whether the object known as arg is equal to the
object known as True.
</quote>

Not at all, it makes perfect sense. X == Y always tests whether the
argument X is equal to the object Y regardless of what X and Y are.

None of these four examples are "equal" to any other.

That's actually wrong, as you show further down.

a = 1
b = (1,)
c = [1]
d = gmpy.mpz(1)

type(a)
a==b False
b==c False
a==d
True

See, a and d are equal.

And yet a==d returns True. So why doesn't b==c
also return True, they both have a 1 at index position 0?

Why should they return true just because the contents are the same? A bag
of shoes is not the same as a box of shoes, even if they are the same
shoes. Since both lists and tuples are containers, neither are strings or
numeric types, so the earlier rule applies: they are different types, so
they can't be equal.

gmpy.mpz(1) on the other hand, is both a numeric type and a custom class.
It is free to define equal any way that makes sense, and it treats itself
as a numeric type and therefore says that it is equal to 1, just like 1.0
and 1+0j are equal to 1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,055
Latest member
RacheleCar

Latest Threads

Top