
Chris Angelico writes:
[fruit] = sscanf(sample, "%*sfruit:%s\n")
I'm warming to this idea. It does hit the sweet spot of doing exactly what you want -- except when it can't do what you want at all. :-) It's concise and quite powerful, applicable to many common use cases. I do have one windowframe of the bikeshed to paint: this is Python, so maybe just "scanf" is a fine name? The first argument can be any iterable of characters, and if an iterator it would leave the iteration pointer where it left off (eg, beginning of next line in 'sample' above). Then the question would be how to use that feature. Specifically, how does scanf deal with the case that the parse fails? Consider while True: fruits.append(scanf(input_file, "%*sfruit:%s\n")[0]) Neither returning a sentinel (presumably None) nor raising a NotFound exception seems palatable. Can it raise StopIteration, perhaps conditional on the first argument having a .__next__?