News for hackers. Stuff that matters.
Re:A Guide to Python's Magic Methods by steve-howard on Saturday March 17, 2012 @02:07PM (#3717663)
>__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.

Re:A Guide to Python's Magic Methods by chernevik on Saturday March 17, 2012 @02:50PM (#3717815)
I 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.

Re:A Guide to Python's Magic Methods by jules on Saturday March 17, 2012 @09:02PM (#3718742)
It would be perhaps be useful to throw an error saying "X object has no attribute Y, did you mean Z instead?".
Re:A Guide to Python's Magic Methods by steve-howard on Saturday March 17, 2012 @10:42PM (#3718943)
That's probably a better idea. In either case being "liberal in what you accept" is a discredited software practice.
Re:A Guide to Python's Magic Methods by mhansen on Sunday March 18, 2012 @01:54AM (#3719236)
being "liberal in what you accept" is a discredited software practice.

What? When did that get discredited?

Re:A Guide to Python's Magic Methods by steve-howard on Sunday March 18, 2012 @01:54PM (#3720273)
Not 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-libe...