Preferred way of defining properties in Python: property decorator or lambda?

For read-only properties I use the decorator, else I usually do something like this:

class Bla(object):
    def sneaky():
        def fget(self):
            return self._sneaky
        def fset(self, value):
            self._sneaky = value
        return locals()
    sneaky = property(**sneaky())


Recent versions of python enhanced the decorator approach:

class Bla(object):
    def elegant(self):
        return self._elegant

    def elegant(self, value):
        self._elegant = value

Leave a Comment