How does the @property decorator work in Python?

后端 未结 13 2070
闹比i
闹比i 2020-11-21 04:49

I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only

13条回答
  •  不思量自难忘°
    2020-11-21 05:47

    This point is been cleared by many people up there but here is a direct point which I was searching. This is what I feel is important to start with the @property decorator. eg:-

    class UtilityMixin():
        @property
        def get_config(self):
            return "This is property"
    

    The calling of function "get_config()" will work like this.

    util = UtilityMixin()
    print(util.get_config)
    

    If you notice I have not used "()" brackets for calling the function. This is the basic thing which I was searching for the @property decorator. So that you can use your function just like a variable.

提交回复
热议问题