[clock] magic methods and their non-introduction
See original GitHub issueIn 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 Clock
s, 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:
- Created 6 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
I am just glad that someone popped into the gitter chat room to make inquiries.
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.