How to teach decorators to Undergrad students? I mean how make the concept crystal clear? Sometimes it becomes complex as :

"functions passing another functions in argument, adding new functionality to existing functions or classes without modifying the existing structure" and using "@" symbol confuses students.

I know decorators are very powerful tools. Any suggestions for teaching them effectively?

  • 1
    $\begingroup$ "Python" isn't really essential here. The same examples work in other OO languages. I'll give some ideas later today, I think. $\endgroup$
    – Buffy
    Apr 28 at 12:40
  • $\begingroup$ In the short term think about teaching Strategy immediately before Decorator. Strategies are a natural thing to decorate. $\endgroup$
    – Buffy
    Apr 28 at 13:01
  • $\begingroup$ I am asking about the concept of decorators.. it is specific to python in my case $\endgroup$ Apr 28 at 14:28

Although it should probably be only one strategy among many, you might teach it through practical examples. In this short program that uses flask_user (and which students can install in just a few minutes) the decorator ensures that only authenticated users can use the route:



There are two somewhat separate concepts of a decorator. I'm guessing that you mean the one related to using "first class" functions in Python or in a more traditional functional language. The other concept is from OO programming and involves the method structure of an object. They are related, of course, since a function in Python is an object.

The first concept is discussed here. The other is in the standard OO design pattern literature in many places. For example, here

But this is an idea for a project that could be implemented in either paradigm.

Imagine a robot that has certain "reversible" action, moving, turning, etc. Suppose that you want to send the robot off on some sort of quest. The quest may be modified along the way as the robot encounters various obstacles or such. Suppose further that, at the end of the quest you want to have the robot return to its original position - a partial "undo" of its actions.

The idea is to have a Strategy for the robot that it "carries" either as an object or as a function (The Strategy Pattern). For each action that you want to undo, have the robot "decorate" its strategy with an additional action that undoes the current action. For example, to undo a "left turn" you execute a "right turn" assuming that the robot will then be facing the opposite direction.

In effect, you build up a stack of decorators that define all of the actions required to return to the base position from wherever it is at the moment.

Each decorator will first execute the immediate undo action and then invoke the decorated item, unwinding the stack. But note that to "undo" all that is required is a single message, not a loop. The structure itself implements the repetition.

Implementation notes:

If you are doing this with novices I have one bit of advice. The strategy and the decorator need exactly the same interface. For the functional idea, that means the same parameter structure, and for the OO version the classes should have the same public method interface. If you break this rule (as pros might need to do on occasion) you will likely confuse the students as to what is essential in the idea.


Your Answer

