Thomas Lotze <[EMAIL PROTECTED]> writes: > A related problem is skipping whitespace. Sometimes you don't care about > whitespace tokens, sometimes you do. Using generators, you can either set > a state variable, say on the object the generator is an attribute of, > before each call that requires a deviation from the default, or you can > have a second generator for filtering the output of the first. Again, both > solutions are ugly (the second more so than the first). One uses > side-effects instead of passing parameters, which is what one really > wants, while the other is dumb and slow (filtering can be done without > taking a second look at things).
I wouldn't call the first method ugly; I'd say it's *very* OO. Think of an object instance as a machine. It has various knobs, switches and dials you can use to control it's behavior, and displays you can use to read data from it, or parts of its state . A switch labelled "ignore whitespace" is a perfectly reasonable thing for a tokenizing machine to have. Yes, such a switch gets the desired behavior as a side effect. Then again, a generator that returns tokens has a desired behavior (advancing to the next token) as a side effect(*). If you think about these things as the state of the object, rather than "side effects", it won't seem nearly as ugly. In fact, part of the point of using a class is to encapsulate the state required for some activity in one place. Wanting to do everything via parameters to methods is a very top-down way of looking at the problem. It's not necessarily correct in an OO environment. <mike *) It's noticable that some OO languages/libraries avoid this side effect: the read method updates an attribute, so you do the read then get the object read from the attribute. That's very OO, but not very pythonic. -- Mike Meyer <[EMAIL PROTECTED]> http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list