[Tutor] Question about the use of None

Roeland Rengelink roeland.rengelink at chello.nl
Fri Jun 11 12:02:41 EDT 2004


Wilfred Y wrote:

> Hi, I came across this keyword None. The document I read says "None is 
> python's way of representing nothing. It evaluates to False when 
> treated as a condition" So I assume, if I do start=None, then it's as 
> good as saying start="" or start=0 Now, the code to represent it's use 
> is pretty confusing. It goes like this: start=None while start != "" 
> start=raw_input("Press enter to exit, or key in a string: ") The 
> confusing part is here, since start = "", then it should never be able 
> to go into the while loop. (Well, at least that's if None is really == 
> "") So to check, I tried this in the interactive window: start=None 
> print start and presto !! It prinst the string None, even though i 
> haven't placed it in double quotes. So it seems None does have a 
> value, which explains why it managed to enter the while loop. Is this 
> correct ?

Hi Wilfred,

I think you answered your own question correctly.

 >>> None != ''
True

None is a value, and that values is different from 0 or '', but it 
behaves just like 0 or '' in a condition.

The sentence 'evaluates to False, when treated as a condition' is 
somewhat misleading, because None 'evaluates to' None

Consider:

 >>> if expression:
...     print 'Condition is True'

where expression can be any Python expression (for example: string!='')

The if-statement evaluates the expression, and prints 'Condition is 
True' if the result of the expression is not one of (False, None, 0, 
0.0, '', [], (), or {}).

I.e.: None, 0, etc are all equivalent to False, as far as the 
if-statement is concerned, while everything else is equivalent to True 
(Since v 2.3 of Python, True and False are also build-in values, just 
like None)

Hope this helps,

Roeland






More information about the Tutor mailing list