-9

I'm studying Python OOP and I arrived at the topic of decorators but the material I'm using for studying doesn't cover it in depth. I post the example code:

class Duck:
    def __init__(self, **kwargs):
        self.properties = kwargs

    def quack(self):
        print("Quaaack!")

    def walk(self):
        print("Walk like a duck.")

    def get_properties(self):
        return self.properties

    def get_property(self, key):
        return self.properties.get(key, None)

    @property
    def color(self):
        return self.properties.get("color", None)

    @color.setter
    def color(self, c):
        self.properties["color"] = c

    @color.deleter
    def color(self):
        del self.properties["color"]

def main():
    donald = Duck()
    donald.color = "blue"
    print(donald.color)

if __name__ == "__main__": main()

Can you help me to understand the importance of decorators? Can you explain in simple words the concept of decorator?

Fabio
  • 2,074
  • 2
  • 24
  • 38
  • 1
    There are many articles on decorators on the internet. E.g. http://thecodeship.com/patterns/guide-to-python-function-decorators/ – Alexander Davydov Jul 05 '16 at 10:24
  • 2
    https://docs.python.org/3/glossary.html#term-decorator, https://docs.python.org/3/reference/compound_stmts.html#function – Ilja Everilä Jul 05 '16 at 10:25
  • 1
    Possible duplicate of [Python - Decorators](http://stackoverflow.com/questions/20945366/python-decorators) – SiHa Jul 05 '16 at 10:31
  • I simply need someone to explain in simple words what a decorator is. (not just comment the code). I don't understand why someone down voted the question. I took a look at a possible duplicate question but in those answers nobody explains in general what a decorator is, so my question is actually unique. – Fabio Jul 05 '16 at 10:41
  • Check teh second answer here - http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python?rq=1 I agree that while fundamentally basic, there is no straight duplicate for this question - at least not one that has popped up in simple searches – jsbueno Jul 05 '16 at 13:02

1 Answers1

1

Let me write some of the things I can simply say about the above code,

Generally decorators are said to be

"Function decorators are simply wrappers to existing functions"

Decorators always wrap an another function, Not to mention that the decorator is itself a function

In the above code you are setting,getting,deleting a property/formally an attribute of a "duck" which is a color.

Decorator always start with the '@'. @property is a predefined decorator function that meant to take your getter,setter and del function of your class

    @property #getter
    def color(self):
        return self.properties.get("color", None)

    @color.setter #setter
    def color(self, c):
        self.properties["color"] = c

    @color.deleter #del
    def color(self):
        del self.properties["color"]

All you have been doing is passing the functions(getter,setter,del) as an argument to the @property

if you written your main it would be like

if __name__ == "__main__": main()  
    donald = Duck() //creating object
    donald.color = "blue" //call the setter method, look its more intuitive!!  (you don't using the donald.color("blue") syntax)
    print(donald.color)//getter

We can have our own decorator also, I highly recommend you to read the suggested link for its various uses and advantages

RaGa__M
  • 2,550
  • 1
  • 23
  • 44