Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Generator Function Names -- is a prefix helpful? [closed]

Most functions are easy to name. Generally, a function name is based on what it does or the type of result it produces.

In the case of a generator function, however, the result could be a iterable over some other type.

def sometype( iterable ):
    for x in iterable:
        yield some_transformation( x )

The sometype name feels misleading, since the function doesn't return an object of the named type. It's really an iterable over sometype.

A name like iter_sometype or gen_sometype feels a bit too much like Hungarian Notation. However, it also seems to clarify the intent of the function.

Going further, there are a number of more specialized cases, where a prefix might be helpful. These are typical examples, some of which are available in itertools. However, we often have to write a version that's got some algorithmic complexity that makes it similar to itertools, but not a perfect fit.

def reduce_sometype( iterable ):
    summary = sometype()
    for x in iterable:
         if some_rule(x): 
             yield summary
             summary= sometype()
         summary.update( x )

 def map_sometype( iterable ):
     for x in iterable:
         yield some_complex_mapping( x )

 def filter_sometype( iterable ):
     for x in iterable:
         if some_complex_rule(x):
              yield x

Does the iter_, map_, reduce_, filter_ prefix help clarify the name of a generator function? Or is it just visual clutter?

If a prefix is helpful, what prefix suggestions do you have?

Alternatively, if a suffix is helpful, what suffix suggestions do you have?

like image 496
S.Lott Avatar asked Nov 18 '10 14:11

S.Lott


People also ask

What does the close () method do to a generator?

The . close() method works by raising a GeneratorExit exception inside the generator function. The exception is generally ignored but can be handled using try and except . Putting the yield expression in a try block we can handle the GeneratorExit exception.

What is a Python generator function?

Python Generator functions allow you to declare a function that behaves likes an iterator, allowing programmers to make an iterator in a fast, easy, and clean way. An iterator is an object that can be iterated or looped upon. It is used to abstract a container of data to make it behave like an iterable object.

Are generators in Python useful?

Generators have been an important part of Python ever since they were introduced with PEP 255. Generator functions allow you to declare a function that behaves like an iterator. They allow programmers to make an iterator in a fast, easy, and clean way.

Is Python open a generator?

Generators in Python — 5 Things to Know (medium.com) using the open() method to open the EEG file will create a file object, which functions as a generator that yields a line of data as string each time. One can probably find easily more of such examples from everywhere on the Internet..


1 Answers

Python dicts have iter* methods. And lxml trees also have an iter method. Reading

for node in doc.iter():

seems familiar, so following that pattern, I'd consider naming the a generator of sometypes sometypes_iter so that I could write analgously,

for item in sometypes_iter():

Python provides a sorted function. Following that pattern, I might make the verb-functions past tense:

sometypes_reduced
sometypes_mapped
sometypes_filtered

If you have enough of these functions, it might make sense to make a SomeTypes class so the method names could be shortened to reduce, map, and filter.

If the functions can be generalized to accept or return types other than sometype, then of course it would make sense to remove sometype from the function name, and instead choose a name that emphasizes what it does rather than the types.

like image 121
unutbu Avatar answered Sep 21 '22 13:09

unutbu