Pythonic emptiness checking

  • Thread starter Filip GruszczyÅ„ski
  • Start date
F

Filip Gruszczyński

One of the Python Zen rules is Explicit is better implicit. And yet
it's ok to do:

if x:
do_sth

when x is string or list. Since it's very comfy, I've got nothing
against though. I am just curious, why is it so?

And one more thing: is it ok to do

if x:

instead of

if x is not None:

Because I often encounter it and would like to know, if I can simplify
it. Especially that I liked similar construction in C/C++.
 
R

r

One of the Python Zen rules is Explicit is better implicit. And yet
it's ok to do:

if x:
   do_sth

when x is string or list. Since it's very comfy, I've got nothing
against though. I am just curious, why is it so?

It also works for numbers and tuples and... All types have a value in
python try the bool() function

And one more thing: is it ok to do

if x:

instead of

if x is not None:

Because I often encounter it and would like to know, if I can simplify
it. Especially that I liked similar construction in C/C++.

Well you could do either or, but i very much like the simplicity of

if <value>:
do this


"Python is simplistic programming bliss. don't thank God, thank
Guido!"
 
B

Bruno Desthuilliers

Filip Gruszczyński a écrit :
One of the Python Zen rules is Explicit is better implicit. And yet
it's ok to do:

if x:
do_sth

when x is string or list. Since it's very comfy, I've got nothing
against though. I am just curious, why is it so?

Because it is explicit (or at least considered as such) that in Python,
an empty list or string (as well as empty tuples, dicts and sets and
numeric zeros, False and None) have a false value in a boolean context.
And one more thing: is it ok to do

if x:

instead of

if x is not None:

Depends on the context. You of course understand that the two
expressions are not equivalent, don't you ?-)
 
S

Sibylle Koczian

Filip said:
One of the Python Zen rules is Explicit is better implicit. And yet
it's ok to do:

if x:
do_sth

when x is string or list. Since it's very comfy, I've got nothing
against though. I am just curious, why is it so?

And one more thing: is it ok to do

if x:

instead of

if x is not None:

Because I often encounter it and would like to know, if I can simplify
it. Especially that I liked similar construction in C/C++.
Depends on what you need. If your x is 0 or 0.0, '', the empty list,
dictionary or set, or a class instance with __len__ 0 or __nonzero__
false, then x is false, but "x is not None" is true.

HTH
Sibylle
 
F

Filip Gruszczyński

Yes, I get the difference. If x is [], than

if x:

won't be executed and

if x is not None:

will be.

Thanks for clarifying.
 
C

Carl Banks

Filip Gruszczyński a écrit :




Because it is explicit (or at least considered as such) that in Python,
an empty list or string (as well as empty tuples, dicts and sets and
numeric zeros, False and None) have a false value in a boolean context.

I think a better answer to this question is: "The Zen of Python is not
called the Cold Hard Rules of Python"; in this case the language goes
against this particluar Zen as it does in many other places.


Carl Banks
 
M

MRAB

Carl said:
I think a better answer to this question is: "The Zen of Python is not
called the Cold Hard Rules of Python"; in this case the language goes
against this particluar Zen as it does in many other places.
"Although practicality beats purity". It's all about balance.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top