Shanshan Pythoner Love CPP

Python Cheatsheet: Decorator

2017-02-24

Why Decorator

Assume we has a function called myfunc, we wanna to call the function in another function. So

In this way, every time you have to use “deco(myfunc)”.

That’s why we use the decorator.

“@deco” means “myfunc = deco(myfunc)”

Decorator with parameter

“addFunc(3, 8) = deco(True)( addFunc(3, 8))”, “myFunc() = deco(False)( myFunc ())”

Inner Decorator

In python, there are three inner decorator: staticmethod, classmethod, property.

  • staticmethod: the difference with method in class is without self. And it can be used without the object.

  • classmethod: the difference with method in class is the first parameter is clf, instead of self.

  • property: to get the values in class.


Comments

Content