4 ms·
Not in Python, because you wouldn't globally override it. You would just override the __getattr__ of the appropriate objects and be done with it, and nothing el
by jerf 17y ago
Not in Python, because you wouldn't globally override it. You would just override the __getattr__ of the appropriate objects and be done with it, and nothing else would be affected. If you want it to be isolated a bit of work with "with" would make it easy.
Though I would go an entirely different direction, personally, something like:
f = self.find
result = maybe_apply((f, ('table', {'class': ...})),
(f, ('td', {'class': 'elem'})),
(f, ('input',)),
(self.get, ('name',)))
Explicit is better than implicit. For bonus points, the "query" is now reusable and programmatically manipulable, which in my experience outweighs the ugly factor.
- bad_user 17y agoWhat do you mean by "reusable"? You're basically using Python as if it were Lisp, which is kind of ugly :)
- jerf 17y agoThe "qeury" has been reified as data. Now you can do whatever you want to it, including assign it to a variable. Sometimes the ugly tradeoff is OK, even in Python. In this case it is still less ugly than the code it is replacing, if you write correct code. Doing one little piddly-ass thing (running one method), then copy/pasting in the same four lines of error handling after every single one, well, it may be aesthetically pleasing on your screen but it's far uglier than what I've shown. Python has many advantages, but it isn't perfect.