This recipe suggests an idiom for property creation that avoids cluttering the class space with get/set/del methods that will not be used directly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
""" Rather than defining your get/set/del methods at the class level, as is usually done, e.g. class MyClass(object): def __init__(self): self._foo = "foo" def getfoo(self): return self._foo def setfoo(self, value): self._foo = value def delfoo(self): del self._foo foo = property(getfoo, setfoo, delfoo, "property foo's doc string") I would like to suggest the following alternative idiom: """ class MyClass(object): def __init__(self): self._foo = "foo" self._bar = "bar" def foo(): doc = "property foo's doc string" def fget(self): return self._foo def fset(self, value): self._foo = value def fdel(self): del self._foo return locals() # credit: David Niergarth foo = property(**foo()) def bar(): doc = "bar is readonly" def fget(self): return self._bar return locals() bar = property(**bar())
Using the standard idiom to create properties unnecessarily clutters the class space with get/set/del methods that will not be called directly by users of your class (although, of course, they could be). Using the above idiom removes the get/set/del methods from the class space by nesting them inside of a function with the same name as the property you will create. This function is then used to create your new property, at which time said function will no longer have any referrers. As such, it will not be accessible to users of your class - only the newly created property will remain accessible.
Following this idiom is, of course, unnecessary as the standard method works just fine. And, in fact, using this idiom for creating properties like bar (above), will seem excessive. Still, it does provide a clean method for restricting the avenues of access to your classes by their users - if there are no get/set/del methods available, then users of your classes will have to use your property, which was your intention.
NOTE: see comment "Just so users are aware" below.