I've come up with a relatively unobtrusive pattern for defining setters. Given the following definition: def propset(prop): assert isinstance(prop, property) def helper(func): return property(prop.__get__, func, func, prop.__doc__) return helper we can declare getters and setters as follows: class C(object): _encoding = None @property def encoding(self): return self._encoding @propset(encoding) def encoding(self, value=None): if value is not None: unicode("0", value) # Test it self._encoding = value c = C() print(c.encoding) c.encoding = "ascii" print(c.encoding) try: c.encoding = "invalid" # Fails except: pass print(c.encoding) I'd like to make this a standard built-in, in the hope the debate on how to declare settable properties. I'd also like to change property so that the doc string defaults to the doc string of the getter. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4