Class methods in standard library that (c|sh)ould be staticmethods

Wed Jan 1 20:05:38 CET 2003

As I understand it, based on the description of a staticmethod, there
are some methods that qualify as static methods this way:
* They are irrelevant to the instance data
* They are meaningful if called on their own (through the class of
course) without any need for creating a class instance.

A staticmethod has also a minor advantage of lower overhead and slightly
faster calls (as verified in 2.2.2 and 2.3 by me) than methods (UIAM
static methods stop being methods and turn into simple functions).

Since static methods are there, anyway, and keeping in mind that many
(at least, *I* did and still do occasionally :) newbies browse the
standard library to learn Python, I suggest we change some of the
library class methods into static methods.  I'm willing do that (ie
provide a patch), if there is some consensus; after all, I already did
that and verified that nothing changed in the standard test suite

At the end of my post I provide a list of the methods that can be turned
into static methods, along with a little reasoning why.  I picked
methods that can be of wider use (for some of which I have written
versions of my own when I shouldn't, since they were in the library...).

Opinions, please.

** List of staticmethod candidates:

- File '', class FieldStorage, method make_file:
# Returns a read-write temporary binary file. (perhaps a
tempfile.TemporaryFile would be more appropriate, though)

- File '', class CGIHTTPRequestHandler, method
# Checks whether argument is an executable.
- File '', class CGIHTTPRequestHandler, method
# Checks whether path argument is a python script.

- File '', class Cmd, method columnize:
# Displays a list of strings as a compact set of columns.

- File '', class ConfigParser, method _interpolate:
# Substitutes %(name) values "recursively" (tries again after
I would drop the underscore in this case, though.

- File '', class AbstractFormatter, method format_roman:
# Returns a number as a Roman numeral.

- File '', class HTMLParser, method unescape:
# Returns its arg unescaping some common HTML "&name;" entities

- File '', class HTMLRepr, method escape:
# Returns its arg escaping '&', '<' and '>'

- File '', class TextDoc, method indent:
# Indents each line in its text arg with its prefix arg

- All encode, decode methods of the Codec classes in the
encodings/<codec>.py files (although str.decode and unicode.encode feel
more appropriate)
TZOTZIOY, I speak England very best,
Real email address: 'dHpvdEBzaWwtdGVjLmdy\n'.decode('base64')

More information about the Python-list mailing list