Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of a function returning a generator

How do you name a function that returns a generator (that is, uses yield foo instead of return foo)?

  • It's definitely not getFoo() because it does not return a value of Foo.
  • It's probably not foos() because I'd rather have an easy-to-distinguish prefix.
  • It's probably not exactly listFoo() because it does not return a list.
  • It's probably not 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.

like image 691
9000 Avatar asked Jan 27 '12 19:01

9000


People also ask

What does a generator function return?

Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).

Does generator function has a return statement?

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.

What is the function of 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.

Does yield return a generator?

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.


1 Answers

I think foo or foos are possibilities. Or, to mimic Python2 dicts' iteritems, you could use iterfoo.

like image 178
unutbu Avatar answered Sep 23 '22 02:09

unutbu