[Tutor] Why begin a function name with an underscore
Richard D. Moores
rdmoores at gmail.com
Tue Aug 28 02:26:26 CEST 2012
I've been going through Steven D'Aprano's pyprimes
(<http://pypi.python.org/pypi/pyprimes/0.1.1a>) and have many
questions. One is why begin a function name with an underscore. He has
several functions that do this. Two are:
def _validate_int(obj):
"""Raise an exception if obj is not an integer."""
m = int(obj + 0) # May raise TypeError.
if obj != m:
raise ValueError('expected an integer but got %r' % obj)
def _validate_num(obj):
"""Raise an exception if obj is not a finite real number."""
m = obj + 0 # May raise TypeError.
if not isfinite(m):
raise ValueError('expected a finite real number but got %r' % obj)
I'm stealing these for their usefulness. But why the underscores?
I think I've also seen elsewhere variable names that also begin with
an underscore, e.g., _a .
Dick Moores
More information about the Tutor
mailing list