Why does the yield function not require parentheses in Python? -


in python, have many times seen yield function used create generator. both , print function technically both perform action of methods because return value. however, during change python 2 python 3, print function gained parentheses normal method call, yield stayed same. also, yield gains yellowish color of reserved keyword while print purple of reserved method. why yield not considered method , colored way along not using parentheses syntax?

(in similar vein, why return lack parentheses?)

let me add more stuff, yield , continue not given parentheses in many other languages well. wanted know makes different other reserved. there many other reserved methods out there parentheses.

so went digging answer. and turns out, there one. pep 255, pep gave yield keyword

q. why new keyword "yield"? why not builtin function instead?

a. control flow better expressed via keyword in python, , yield control construct. it's believed efficient implementation in jython requires compiler able determine potential suspension points @ compile-time, , new keyword makes easy. cpython referrence implementation exploits heavily, detect functions are generator- functions (although new keyword in place of "def" solve cpython -- people asking "why new keyword?" question don't want new keyword).

q: why not other special syntax without new keyword? example, 1 of these instead of "yield 3":

   return 3 , continue    return , continue 3    return generating 3    continue return 3    return >> , 3    generator return 3    return >> 3    return << 3    >> 3    << 3    * 3 

a: did miss 1 ? out of hundreds of messages, counted 3 suggesting such alternative, , extracted above them. nice not need new keyword, nicer make yield clear -- don't want have deduce yield occurring making sense of senseless sequence of keywords or operators. still, if attracts enough interest, proponents should settle on single consensus suggestion, , guido pronounce on it.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -