How do you name a function that returns a generator (that is, uses yield foo
instead of return foo
)?
getFoo()
because it does not return a value of Foo.foos()
because I'd rather have an easy-to-distinguish prefix.listFoo()
because it does not return a list
.iterateFoo()
because this prefix is too long.What's your preferred solution?
Update:
While foos()
may be a perfectly good solution in some cases, note how method names tend to begin with a verb. The verb conveys the idea that this is a method, not a data field, and thus helps readability. If possible, I'd prefer a solution that makes it easy to tell a method from a data field.
Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).
A return statement in a generator, when executed, will make the generator finish (i.e. the done property of the object returned by it will be set to true ). If a value is returned, it will be set as the value property of the object returned by the generator.
Generators create electrical energy to power appliances running on electricity by burning the fuel it uses for producing electricity. The generator includes different elements like a fuel system, engine, voltage regulator, alternator, a control panel, lubrication system, cooling, and exhaust system.
When you use a yield keyword inside a generator function, it returns a generator object instead of values. In fact, it stores all the returned values inside this generator object in a local state.
I think foo
or foos
are possibilities. Or, to mimic Python2 dicts' iteritems
, you could use iterfoo
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With