简体   繁体   中英

Ruby generators vs Python generators

I've been researching the similarities/differences between Ruby and Python generators (known as Enumerators in Ruby), and so far as i can tell they're pretty much equivalent.

However one difference i've noticed is that Python Generators support a close() method whereas Ruby Generators do not. From the Python docs the close() method is said to do the following:

Raises a GeneratorExit at the point where the generator function was paused. If the generator function then raises StopIteration (by exiting normally, or due to already being closed) or GeneratorExit (by not catching the exception), close returns to its caller."

Is there a good reason why Ruby Enumerators don't support the close() method? Or is it an accidental omission?

I also discovered that Ruby Enumerators support a rewind() method yet Python generators do not...is there a reason for this too?

Thanks

This documentation for the rewind method is a little scarce on details. But in order to "start over" the generator would have to do one of two things:

  • remember its complete output, repeat that output once rewound, then resume what it was doing before
  • reset its internal state in a way that causes the same output to be repeated without other unwanted side effects

The second of these is not always possible; for example, if the generator emits byte buffers from the network, the output is not entirely a function of internal state. But any generator that uses the first technique must necessarily build up a larger and larger buffer in memory as it is used. Such generators offer little performance benefit over lists.

Therefore, I conclude that the Ruby rewind method must be optional and not always supported by a concrete enumerator class. So if the Python designers value the Liskov substitution principle , that would lead them not to require such a method in all generators.

生成器是基于堆栈的,Ruby的枚举器通常是专用的(在解释器级别)而不是基于堆栈的。

Ruby's Enumerator's use StopIteration class internally, see How do Enumerators work in Ruby 1.9.1?

(it's just wrapped if you use it in a for each call). So I'd say they'are a rather close. That being said, I'm not sure what a close method on an enumerator should do, exactly...cleanup, perhaps? (Python's generators probably would benefit from a rewind--note well that in Ruby, some enumerators don't respond to rewind, so they raise an exception when you call that method).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM