Generator naming convention?

A

aurora00

I use generators a lot. E.g.


def gen_words(text)
... parse text ...
yield each word in text

for word in gen_words(text):
print word


I don't like the name gen_xxx() very much. Looking for some inspiration
to name generators. Here are some of my ideas:

enumerate_words
gen_words
generate_words
parse_words
walk_words

Any idea? Do you have a naming convention for generators?

wy
 
D

Diez B. Roggisch

I use generators a lot. E.g.


def gen_words(text)
... parse text ...
yield each word in text

for word in gen_words(text):
print word


I don't like the name gen_xxx() very much. Looking for some inspiration
to name generators. Here are some of my ideas:

enumerate_words
gen_words
generate_words
parse_words
walk_words

Any idea? Do you have a naming convention for generators?

No. From my POV, a generator is not different from any other method
returning a list. I don't care if the implementation is

def words():
return ["dies", "ist", "das", "Haus", "vom", "Nikolaus"]

or

def words():
for w in ["dies", "ist", "das", "Haus", "vom", "Nikolaus"]:
yield w


Regards,

Diez
 
I

infidel

Any idea? Do you have a naming convention for generators?

Sometimes I use the prefix 'iter', like dictionaries have .items() and
..iteritems(). sometimes I use 'x', like range() vs. xrange(). You
could simply use 'i' like some of the functions in the iteritems module
(imap(), izip(), etc). I guess it depends on the project, what you're
doing, your mood at the moment, and the alignment of Jupiter and
Mercury in Aquarius.
 
A

aurora00

iter- clicks for me, thanks :)

wy

Sometimes I use the prefix 'iter', like dictionaries have .items() and
.iteritems(). sometimes I use 'x', like range() vs. xrange(). You
could simply use 'i' like some of the functions in the iteritems module
(imap(), izip(), etc). I guess it depends on the project, what you're
doing, your mood at the moment, and the alignment of Jupiter and
Mercury in Aquarius.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
I use generators a lot. E.g.


def gen_words(text)
... parse text ...
yield each word in text

for word in gen_words(text):
print word


I don't like the name gen_xxx() very much.

Nor do I.
Looking for some inspiration
to name generators. Here are some of my ideas:

enumerate_words
gen_words
generate_words
parse_words
walk_words

Any idea? D

Any of these names have different (somewhat implied) semantic to me. I'd
expect enunmerate_words to yield (order, word) pairs, parse_words to
yield tokens, walk_words to traverse a tree of words, etc

As you see, this is much about what it does than what it is. And FWIW,
do you prefix function names s with fun_ ?
you have a naming convention for generators?

No.
 
G

Georg Brandl

I use generators a lot. E.g.


def gen_words(text)
... parse text ...
yield each word in text

for word in gen_words(text):
print word


I don't like the name gen_xxx() very much. Looking for some inspiration
to name generators. Here are some of my ideas:

enumerate_words
gen_words
generate_words
parse_words
walk_words

Any idea? Do you have a naming convention for generators?

If it's not a short one where you can see the whole body immediately,
I think it's more important to mention the generator-ness in the
docstring.

Georg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top