Quick question about None and comparisons

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Quick question about None and comparisons

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/


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: Quick question about None and comparisons

r t-4
On Nov 24, 7:52 pm, "Giampaolo Rodola'" <[hidden email]> wrote:

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

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: Quick question about None and comparisons

Chris Rebert-3
In reply to this post by Giampaolo Rodola'
On Mon, Nov 24, 2008 at 5:52 PM, Giampaolo Rodola' <[hidden email]> wrote:

> 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
--
Follow the path of the Iguana...
http://rebertia.com

> Thanks in advance
>
> --- Giampaolo
> code.google.com/p/pyftpdlib/
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: Quick question about None and comparisons

Giampaolo Rodola'
In reply to this post by Giampaolo Rodola'