holdenweb / holdenweb/int-py-notes

Decorator code results do not agree with commentary

Open
#4 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
65
Forks
55
PR merge metrics
No merged PRs in 30d

Description

In _introducing-decorators.ipynb_ we apply a class decorator, `barking()`, to two instances, the first of a class with one applicable method, the second of a subclass of that class with two further applicable methods. This succeeds with the first class (the one with only one method) but fails with the second for reasons described below.

Although the commentary makes it appear the (very lightweight) testing performed succeeds, closer examination of the output reveals that the two additional methods are actually calling the same original method (`wag()`), and the the original code of the decorator has a bug.

This issue is caused by the use of a decorator-local variable to wrap each non-dunder method. Since the same variable is used to wrap each method, and since all methods are handled in a single call to the decorator, the wrapped functions must necessarily all be the same.

This explains why both `d3.wag()` and `d3.sniff()` print "a dog_3 is happy" - the `wag()` method is the last to be processed by the decorator, and is thus the function called by all decorated methods.

Note also that this example demonstrates that only locally-defined methods appear in the class's `__dict__` - the inherited `shout()` method is not wrapped a second time.

Please comment and reassign to me for further discussion.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.