Slots are … well, they’re a superficially simple concept but a horribly complicated one when you get under the covers (as I’m having to do at the moment in gpiozero). Anyway, they’re the sort of thing I’d normally avoid (because of the underlying complexity and the way it can bite you in the ass when you least expect it).
However, at a recent picademy I was having a chat with an Australian chap (James - if you’re reading this, my humble apologies - I can’t remember your last name!) who made a compelling case that one of the nastiest bits in Python for learners is the fact that you can set arbitrary attributes on instances without error. So, in his case he was playing around with picamera and wound up setting something incorrectly, let’s say
resoluton instead of
resolution (again, I can’t remember!) and of course the code didn’t complain at all when run, it just didn’t do the right thing.
Now, fixing the attributes of a class in an API (especially one like
__slots__ certainly complicates things for the hardened pro (there are certain sub-classing scenarios it can screw with, particularly if you get into multiple inheritance) but let’s face it: no newbie is going to care about that, and I doubt most advanced coders would be bothered either (very few people ever subclass
PiCamera). But, it suddenly makes it throw an error every time you set an attribute with a spelling mistake instead of silently doing the wrong thing which is much better from a learning perspective.
So, I was utterly convinced, and set about applying this to the next project I was hacking on (gpiozero … sorry James, I’ll get around to picamera when I can!) and, lo and behold, the underlying complexities of slots have just bitten me in the ass. I’m not going to drop the idea of having it throw
AttributeError when misspelt attributes are set, because I’m convinced it’s a good one, but I’m probably going to have to come up with another way of doing it!
Anyway, that’s quite enough rambling from me! On with the hacking…
P.S. Can’t say I’ve spotted any serious ugliness in Explorer HAT (except the
help() mechanism, but then that’s deficient in gpiozero too because, so far, I’ve failed to convince Ben that reST is the only sane option for “proper” API docs ;). Haven’t looked under the hood of Pibrella yet though!