Usually you don't write setters / getters at all. There is no point, since python does not prevent anyone from accessing attributes directly. However, if you need logic, you can use property s
class Foo(object): def __init__(self, db): self.db = db @property def x(self): db.get('x') @x.setter def x(self, value): db.set('x', value) @x.deleter def x(self): db.delete('x')
Then you can use these property methods in the same way as for the base attribute value:
foo = Foo(db) foo.x foo.x = 'bar' del foo.x
source share