3 ms·
>__getattr__(self, name) > ... This can be useful for catching and redirecting common misspellings ... I can think of only one good reason to redirect spel
by steve-howard 15y ago
>__getattr__(self, name)
> ... This can be useful for catching and redirecting common misspellings ...
I can think of only one good reason to redirect spelling errors, and that's if you are a widely-used library and you want to support both "color" and "colour" (and similar Britishisms). Other than that, you're just asking for a headache.
- chernevik 15y agoI am but an egg . . . and while I agree this stuff is great, it is magic and I think it helps to write code so your usage is explicit. Sometimes all that's needed is a hint that something is Not Normal, so the code reader is alerted to look carefully. A test showing Foo(4) * Bar('grandma') == 'nice' can at least flag and often explain whatever mysteries you've created. If you don't put the tests in doc strings with doc tests, at least put a reference to them in the module so the reviewer knows to look for them. Stuff like __getattr__ is particularly tricky. I also try to end __getattr__ code with an AttributeException, which I find really helps debugging. I like using this to dispatch calls to objects better built to fill them, but I try to do so explicitly within the __getattr__ code to give reviewers a clue as to what's going on. For example, I'll check the attribute name against a list before passing it on to the target object. Even if I intent to pass _everything_ to a target, I'll first check the target for the passed attribute with getattr(target, attribute name, False), so that failed passthroughs stay with the object calling them. (You can generate messages indicating the problem -- "Can't find 'RIAA' in object 'Nice'" and pass those to an AttributeException.) Ideally any string validly usable as an instance attribute should be findable somewhere in the code by grep.
- jules 15y agoIt would be perhaps be useful to throw an error saying "X object has no attribute Y, did you mean Z instead?".
- steve-howard 15y agoThat's probably a better idea. In either case being "liberal in what you accept" is a discredited software practice.
- mhansen 15y agobeing "liberal in what you accept" is a discredited software practice. What? When did that get discredited?
- steve-howard 15y agoNot entirely discredited; I spoke too strongly. It's not a good idea to be more liberal than the situation calls for; here's a discussion on the subject: http://programmers.stackexchange.com/questions/12401/be-liberal-in-what-you-accept-or-not http://programmers.stackexchange.com/questions/12401/be-libe...