Say you have a public method in Python whose primary purpose is to retrieve the value of an underlying data attribute (i.e. internal backing store). The method may have lazy evaluation logic, etc. A property is an example of such a method.
Then it is natural to use the same name for the method and data attribute, except for an underscore prefix for the data attribute. For example--
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
(from Python's "property" documentation)
But what are some preferred conventions if the method is for internal use and so is itself prefixed with an underscore? Prefixing the backing store with two leading underscores would invoke name mangling and so is not ideal.
Two possibilities might be--
def _get_x(self):
return self._x
def _x(self):
return self._x_
Python style says the second (appending an underscore), though, should only be used to avoid conflicts with reserved keywords.