Code Challenge 24 - Use Dunder / Special Methods to Enrich a Class - Review

PyBites, Mon 26 June 2017, Challenges

classes, codechallenges, dunders, guest, magic methods, operator overloading, polymorphism, special methods

In this article we review last week's Use Dunder / Special Methods to Enrich a Class code challenge we coupled to our guest post on Dan Bader's blog.

Account class

First of all you can follow along with the code in the article here (class and notebook). One thing we learned from the comments on the article is that __reversed__ should reverse the normal iteration. So newest to oldest, not by transaction amount (fix)

Submissions

As there were no PR submissions other than ours we keep it short.

We made a Developer class pretty similar to the Account class example.

Funny fact is it was meant for our new Karma Bot but we implementing it without using dunders! So it is good to note that you don't always need them.

Even a word of caution is warranted as pointed out by _seemethere on the article's Reddit thread:

I would say that you should be careful with magic methods.

On one hand they can be extremely useful and extend your objects and on the other hand they can make using your library very difficult and hard to understand.

Use them where you feel as though they make sense and not just because it would be cool to use.

Useful tool, powerful and elegant, but use wisely.

Next

Stay tuned for this week's code challenge where we get you to code up a weekly digest email of now playing/ upcoming movies or series.

By the way there is no deadline to these challenges, you can start any challenge at any time.

Just follow our instructions and start coding. Have fun!


Keep Calm and Code in Python!

-- Bob and Julian

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book