Newbies: Re: Returning none

Skip Montanaro skip at mojam.com
Fri Sep 3 08:33:09 EDT 1999


    Paul> #1. a=list.sort("abc"): "None object has no attribute foo" 

For those people who like to think that sort returns a new list, the sort
method could be modified to return the list, but this would make people
think that a and list pointed to different objects.  Then they'd complain
that when modifying the object referred to by list, the object referred to
by a also changed.

It's worth noting is that those people who are clamoring for a distinction
between functions and procedures will have to figure out how to implement
procedures in the C runtime, not just at the Python level.

    Paul> #2. alert( print_traceback() ): "None"

This seems like a semantic problem on the programmer's part more than
anything.  You're clearly asking the function to do something by
side-effect.  Why would you also expect it to return a useful value?  If I
wanted the traceback in a return value I'd expect the function to be named
"get_traceback" or "extract_tb" or something similar.

    Paul> #3. Forgetting to return a value.

This is precisely the case that the default None return helps with.  If the
programmer sees "None" when popping up the dialog box during testing, it's a
clear indication to go back and look at the function that generated the data
the dialog box is displaying.

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/~skip/
847-971-7098   | Python: Programming the way Guido indented...





More information about the Python-list mailing list