Suppose you have an open method in Python whose main purpose is to retrieve the value of the underlying data attribute (i.e. internal backup storage). A method may have lazy evaluation logic, etc. A property is an example of such a method.
Then itβs natural to use the same name for the method attribute and the data, except for the 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 the Python Documentation "Property" )
But what are some preferred conventions if the method is used internally and therefore has an underscore prefix? The backup storage prefix with two leading underscores will cause name manipulation and is therefore not ideal.
Two possibilities are possible -
def _get_x(self): return self._x def _x(self): return self._x_
Python's style says that the second (adding underscores), however, should only be used to prevent conflicts with reserved keywords.
python properties
cjerdonek
source share