Pythonic alternative to dict-style setter?
问题 People tend to consider getters and setters un-Pythonic, prefering to use @property instead. I'm currently trying to extend the functionality of a class that uses @property to support a dict: class OldMyClass(object): @property def foo(self): return self._foo @foo.setter def foo(self, value): self.side_effect(value) self._foo = value class NewMyClass(object): @property def foo(self, key): # Invalid Python return self._foo[key] @foo.setter def foo(self, key, value): # Invalid Python self.side