469,927 Members | 1,784 Online

# test for None

How do you test for a function that returns nothing, and why doesn't
this work? Shouldn't X have to be either None or not?
x = None
for x in []:

.... if x is None:
.... print "X is None"
.... else:
.... print "X is not None"
....

Thanks,

rick

Apr 19 '06 #1
4 1817
BartlebyScrivener wrote:
How do you test for a function that returns nothing, and why doesn't
this work? Shouldn't X have to be either None or not?
x = None
for x in []: ... if x is None:
... print "X is None"
... else:
... print "X is not None"
...

No, because you're iterating over an empty list, which does nothing:
for x in []: print 'hi' ....

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
The doors of Heaven and Hell are adjacent and identical.
-- Nikos Kazantzakis
Apr 19 '06 #2
I think I was trying for something like this, where the intervening for
iterates over a function that may or may not produce nothing:
x = None
for x in []: print x
if x is None:

print "x is still none because nothing happened"

Thanks,

rick

Apr 19 '06 #3
"BartlebyScrivener" <rp*******@gmail.com> writes:
How do you test for a function that returns nothing,
A function returns a single value (which may be a container for other
values). By default, with no 'return' statement, it returns the None
object.

What is it you're trying to do?
and why doesn't
this work? Shouldn't X have to be either None or not?

Yes.
x = None
Assigns the name 'x' to the None object.
for x in []:

Iterates over an empty list, which results in zero iterations;
i.e. does nothing.

Have you followed the tutorial through, running and understanding each
example, to get the basics of Python covered?

<URL:http://docs.python.org/tut/>

--
\ "I was trying to daydream, but my mind kept wandering." -- |
`\ Steven Wright |
_o__) |
Ben Finney

Apr 19 '06 #4
>> Have you followed the tutorial through, running and understanding each
example, to get the basics of Python covered?

Could be high time to try that again. At first it was way too much, so
I've been doing Dive Into Python and some others. But I will try the
Tutorial again, at least up to classes. Then I'm sure I'll have to

Thanks for your patience.

rick

Apr 19 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.