str(None) == '' ?

Gordon McMillan gmcm at hypernet.com
Thu Jun 1 14:54:17 EDT 2000


Eduard Hiti <eduard.hiti at t-online.de> wrote:

>I would like to suggest that
>
>    str(None)
>
>should NOT return the string 'None', since most of the time, it 
complicates
>things.
>
>Then code like
>
>    value = str(get_stuff())
>    if value:
>        # Do something to value
>
>would not stumble over the occasional None.

I think the complication is really in what get_stuff() returns (or in 
what you *want* it to return). Unless badly coded, get_stuff() 
probably means something different when it returns None rather 
than ''.

It's not really str's job to hide that difference. If you want to do so, 
then you could use:

 value = str(get_stuff() or '')

>
>At least one of the string converting functions repr|str should 
return a
>empty string on None.

Definitely not repr, since that would voilate eval(repr(x)) == x.

>Is there a reason for str(None) == 'None' that I'm missing?

For those situations where None has a contextual meaning, and you 
don't want conversion to a string to hide that.

- Gordon




More information about the Python-list mailing list