Quick question about None and comparisons

G

Giampaolo Rodola'

Sorry for the title but I didn't find anything more appropriate.
To have a less verbose code would it be ok doing:

if a > b:

....instead of:

if a is not None and a > b:

....?
Is there any hidden complication behind that?
Thanks in advance

--- Giampaolo
code.google.com/p/pyftpdlib/
 
R

r

Sorry for the title but I didn't find anything more appropriate.
To have a less verbose code would it be ok doing:

if a > b:

...instead of:

if a is not None and a > b:

...?
Is there any hidden complication behind that?
Thanks in advance

--- Giampaolo
code.google.com/p/pyftpdlib/

you are doing 2 different things there
the first only ask "IS a greater than b?"
the second ask "IF a IS NOT none and a IS greater than b"
one is not shorthand for the other in all circumstances!
 
C

Chris Rebert

Sorry for the title but I didn't find anything more appropriate.
To have a less verbose code would it be ok doing:

if a > b:

...instead of:

if a is not None and a > b:

...?
Is there any hidden complication behind that?

Yes. In Python 3.0, doing non-equality comparisons with None will
raise a TypeError because it's nonsensical to ask whether two objects
of unrelated types are less than or greater than each other (e.g Is
[1,2] > "ab" ?).
Assuming `a` will never take on a value considered boolean false, you
can do `if a and a > b:` instead.

Cheers,
Chris
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top