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

前端 未结 3 1245
灰色年华
灰色年华 2020-12-24 12:47

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.fiel         


        
3条回答
  •  盖世英雄少女心
    2020-12-24 13:34

    Don't use lambdas for this. The first is acceptable for a read-only property, the second is used with real methods for more complex cases.

提交回复
热议问题