python-distilled icon indicating copy to clipboard operation
python-distilled copied to clipboard

7.19 - NotImplementedError in NoiseMixin-Base Class throws error bc of MRO

Open haimcqueen opened this issue 2 years ago • 1 comments

Thank you so much for your book, it's nothing short of amazing.

On to the issue - I am not sure if I just didn't understand correctly or if it actually is an issue: At the end of chapter 7.19 you talk about design guidelines. The second one is about implementing a base mixin class. In the exampleNoiseMixin raises an NotImplementedError. So when I call AnnoyingLoudCyclist.__mro__ with

class AnnoyingLoudCyclist(AnnoyingMixin, LoudMixin, Cyclist): pass like before, I get the following output:

(<class '__main__.AnnoyingLoudCyclist'>, <class '__main__.AnnoyingMixin'>, <class '__main__.LoudMixin'>, <class '__main__.NoiseMixin'>, <class '__main__.Cyclist'>, <class 'object'>)

NoiseMixin comes before Cyclist in the MRO which means that when I initiate an instance of the class and call the noise method, I always get the NotImplementedError although the Cyclist class implemented the noise method.

Again, thanks so much for writing this book.

haimcqueen avatar Mar 23 '23 08:03 haimcqueen

Glad you like the book! Let me look at this. Interesting. I'm inclined to refine that advice to only do this if the existence of a noise() method (elsewhere) can't be guaranteed.

dabeaz avatar Mar 23 '23 14:03 dabeaz