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

Posted by PyBites on Mon 26 June 2017 in Challenges • 2 min read

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


See an error in this post? Please submit a pull request on Github.

Join our community and grab our Become a Better Python Developer cheat sheet. Learn Python. Receive bonus material. Challenge yourself!