share function argument between subsequent calls but not between class instances!
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Mon Feb 20 17:18:34 EST 2006
On Mon, 20 Feb 2006 08:52:09 +0000, Duncan Booth wrote:
> Ben Finney wrote:
>
>> Duncan Booth <duncan.booth at invalid.invalid> writes:
>>> If you intend to only use the default some of the time, and at other
>>> times pass in a different list, then save the 'default' in the
>>> instance and use a special marker value to indicate when you intend
>>> the default to be used:
>>
>> 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. If I
cared enough (I don't) I would re-write the counter to analyse all the
default values. But by eye-balling the function lines, the impression I
get is that the remaining 60% of default values are divided unequally
between dozens of different defaults. Some random examples:
"rb", "", "Prompt: ", -1, 0, 1, 3, sys.maxint, (), [].
My gut-feeling would be, 40-odd percent None, 40-odd percent for small
ints (-1, 0, 1, ..?), the remainder split between everything else.
If anyone cares to look at my quick and dirty source code, it is attached
following my signature.
--
Steven.
* * *
"""Rough and ready script to analyse the Python standard library
and count function definitions that use None as a default argument.
"""
from __future__ import division
import os
location = "/usr/lib/python2.3/"
file_count = 0 # number of files successfully opened
func_count = 0 # number of function definitions
default_count = 0 # number of functions with a default value
none_count = 0 # number of functions with None as a default value
for name in os.listdir(location):
if name.endswith(".py") and os.path.isfile(location+name):
try:
fp = file(location+name, "r")
except IOError:
continue
file_count += 1
lines = fp.readlines()
fp.close()
for line in lines:
line = line.strip()
if line.startswith("#"):
continue
elif line.startswith("def "):
func_count += 1
default_count += line.count("=")
none_count += line.count("None")
# if line.count("="): print line
# Report results found:
print "%d .py source files were opened." % file_count
print "%d function or method definitions were found." % func_count
print "These functions included at least %d arguments with default values." % default_count
print "%d or %e%% used None as the default value." % \
(none_count, none_count/default_count*100)
More information about the Python-list
mailing list