share function argument between subsequent calls but not between class instances!

Duncan Booth duncan.booth at invalid.invalid
Tue Feb 21 08:31:45 EST 2006


Steven D'Aprano wrote:

>>> The most common idiom for such a marker is the None value.
>>> 
>> 
>> Can you provide any firm evidence that using None is more common?
> 
> 
> Yes, I wrote a quick and dirty script to roughly count the default
> values in the Python 2.3 standard library. Here are my results:
> 
> $ python default_counter.py
> 185 .py source files were opened.
> 4437 function or method definitions were found.
> These functions included at least 1228 arguments with default values.
> 529 or 4.307818e+01% used None as the default value.
> 
> So, roughly 40% of default values in the standard library are None. 

Fair enough, although I would point out that you haven't made any attempt 
to distinguish those cases where None is being used as a marker from the 
cases where it is being used as a value in its own right or a flag to 
control the function logic.

The marker cases do seem to be the most common but there are plenty of 
other cases:

e.g. base64.b64encode & base64.b64decode avoid part of the code if passed 
None, but don't actually substitute another value in place of the default.

cgi.FieldStorage has methods getvalue, getfirst where the default=None is 
simply that: the default to be returned. The make_file method has a 
defaulted argument which it doesn't use at all.

Also, most of the standard library predates a time when you could create a 
unique marker value just by calling 'object()'. When it was written None 
was by far the simplest option even in cases where a separate marker value 
might have been more appropriate.



More information about the Python-list mailing list