question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[clock] magic methods and their non-introduction

See original GitHub issue

In gitters exercism/support-channel there was a student asking for help with the exercise, because the README wasn’t clear enough for him. We were able to help him with that.

While @kotp discovered via pair programming that already the previous exercises weren’t properly tested but just printing to stdout, he teached the student the fundamentals of testing before submitting and I tried to do the exercise myself, since it was missing anyway. Thereby I discovered the following:


The tests are using the built-in str(), which does call into __str__(), which again has a default implementation to just proxy to __repr__(). Even if I have used pythion here and there, I never really learned it, and I had to lookup what str() does. But it was hard to find in the internet how I can implement an object that does respond properly. I didn’t find anything, so I took a look at the example implementation. There __repr__() was used.

What I was able to find before, was that __repr__() should return a string which is valid python to construct a similar object, so repr(Clock(10, 0)) should return 'Clock(10, 0)', not '10:00'.

So not only, that there is nothing that explains the student how to make the first tests to pass with an actual value instead of None, he is even introduced to bad practice when he looks up the exercises example implementation.


Later on, some tests will fail with some funky Object-Reference stuff printed on the screen. Again, it took me a while to figure out, that some detail in the tests have changed. Instead of comparing the str()ified Clocks, all of a sudden, we compare them directly.

So I had to figure out which method to implement next. This time I looked into the example directly, and found __eq__(), which I implemented straight.


To make the students experience much better I’d suggest to move this exercise to a later point or provide a better stub, which contains both methods mentioned above, alltogether with an explaining comment. Also not returning an arbitrary value or None, but throwing an exception, which clearly states “This method is not implemented, it’s your task to do so”. In .Net there is an NotImplementedException for such cases.

In the same step, I’d also suggest to expand the stubbed __init__() as well to take three positionals instead of the current version which only takes one. Understanding the errormessage the test gave me took a while again.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
kotpcommented, Mar 23, 2017

I am just glad that someone popped into the gitter chat room to make inquiries.

0reactions
behrtamcommented, Mar 23, 2017

While the other issue mentions the str/repr part and that it is too early in the track it acutely aims at making it even more complex. As no one really started to work on that it got lost that the exercise is still in a very wrong spot. So, it was good that you brought the attention back to those problems. I hope we didn’t scare too many people.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Guide to Python's Magic Methods « rafekettler.com
A guide to all the Magic Methods in Python. ... Now would be a good time to note that you don't have to...
Read more >
Magic or Dunder Methods in Python - TutorialsTeacher
Built-in classes in Python define many magic methods. Use the dir() function to see the number of magic methods inherited by a class....
Read more >
9. Magic Methods | OOP | python-course.eu
Python Tutorial: Magic methods and operator overloading with examples. ... We have a complete listing of all the magic methods a little ...
Read more >
Introduction to Python Magic Methods in Python Classes
Let's check out some of the popular magic methods. We'll discuss these methods based on their object usefulness (an object is an instance...
Read more >
Magic Methods - Cambridge Spark
What are magic methods? They are special methods describing how a certain objects should behave. They are always surrounded by double underscores (e.g. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found