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

decorator, lambda, properties, python

Solution

The decorator form is probably best in the case you've shown, where you want to turn the method into a read-only property. The second case is better when you want to provide a setter/deleter/docstring as well as the getter or if you want to add a property that has a different name to the method it derives its value from.

Problem

Which is the preferred way of defining class properties in Python and why? Is it Ok to use both in one class? ``` @property def total(self): return self.field_1 + self.field_2 ``` or ``` total = property(lambda self: self.field_1 + self.field_2) ```

Original source

Related problems