Python Object-Oriented Python Advanced Objects Constructicons

Kohei Ashida
Kohei Ashida
4,276 Points

Is my understanding correct on what is "decorator"?

Applying what Kenneth said in the video as follows to the coding in this video, the class method decorator modifies the expectation of what the class Bookcase would do to create a list of books. Is this correct?

A decorator is more or less a function that takes another function, does something with it, and then usually returns that function. The class method decorator, for example, does some modifying of the expectations of Python's object class. Which all of our classes are inherited from, so class methods can work how they do.

1 Answer

Steven Parker
Steven Parker
194,962 Points

You're right, and in particular a "@classmethod" decorator causes a method that would normally use a "self" parameter to refer to an instance to use a "cls" parameter to refer to the class itself instead.