Why return None?

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Thu Aug 26 12:04:11 EDT 2004


Antoon Pardon wrote:

>>If somelist.sort() returned the list there could not possibly be one
>>obvious way to do it, since both
>>
>>print somelist.sort()
>>
>>and
>>
>>somelist.sort()
>>print somelist
> 
> 
> Then python has already deviated from the one obvious way to do it.
> I can do:
> 
>   a = a + b   vs    a += b.
> 
> or
> 
>   a = b + c   vs     a = ''.join(b,c)

a = ''.join((b,c))

join doesn't join all it's arguments; it joins all elements from it's 
one argument, which should be a sequence. That's a detail though.

> 
> The difference between
> 
>   print somelist.sort()
> 
> and
> 
>   somelist.sort()
>   print somelist
> 
> 
> is IMO of the same order as the difference between
> 
> 
>   print a + b
> 
> and
> 
>   r = a + b
>   print r

Not IMO.

print somelist.sort()

has a side-effect (somelist is sorted), which is not clearly visible if 
used that way.

a + b doesn't have any side-effects.
IMO, 'print somelist.sort()' is more equivalent to 'print a += b', which 
also doesn't work (fortunately).

-- 
"Codito ergo sum"
Roel Schroeven



More information about the Python-list mailing list