mercredi 28 janvier 2015

Why do python generators and functions share the "def" keyword?


Consider the following:



def some_function():
return 1

def some_generator():
yield 1


In the code above, some_function is a function, while some_generator is a generator. They look quite similar.


The problem that I have when reading code is that I need to scan through every line in a "function" looking for the yield keyword before I can determine whether it is actually a function or a generator!


It just seems to me that using a different keyword for generators would make more sense, e.g.:



gen some_generator():
yield 1


What are the merits of using the def keyword for both generators and functions? Why has a new keyword is not been introduced to separate functions and generators?





Aucun commentaire:

Enregistrer un commentaire