469,927 Members | 1,784 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

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
return to the books.

Thanks for your patience.

rick

Apr 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Kamilche | last post: by
reply views Thread by Remy Blank | last post: by
18 posts views Thread by alex | last post: by
reply views Thread by Jussi Mononen | last post: by
6 posts views Thread by Ben Finney | last post: by
5 posts views Thread by Sakcee | last post: by
2 posts views Thread by Netkiller | last post: by
2 posts views Thread by karoly.kiripolszky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.