The generator function of Javascript needs an extra asterisk, but Python does not need an extra identity. Why is it designed like this?
A Javascript generator function looks like this:
def gen(n):
for i in range(n):
yield i
>>> list(gen(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
python does not require additional identification when declaring. Just use yield
directly.
both languages use yield
keywords, and their working principles are almost the same. It can even be said that Javascript is copied python. So why did Javascript add an extra asterisk instead? What"s the meaning of this asterisk? Why is it designed like this? Is Javascipt too special, or is python too special?
I believe there is a reason for this. After all, the formulation process of ES2015 is quite harmonious (literally)
https://stackoverflow.com/que.
< hr >
finally get idle and perfect the answer.
One of the principles that
ES has always adhered to is completely to avoid breaking changes,. After all, the browser version of the user is not something that programmers can interfere with. If the user's upgrade causes the old code of some websites to fail, the organizing committee will certainly be sprayed to death. This is very different from other languages. So Compatibility is the primary reason, and the new version of ES must be fully compatible with the old version of ES.
Readability and Generality are both additional effects. Generality is actually a pseudo requirement, empty Generator can be replaced by Generator with 0 or 1 yield statement, and the latter is more readable than the former. Readability ES is really better than Python. At least I can tell if it's a Generator or Normal Function, by just reading the function header, but the advantage is only obvious when reading long code. These alone are not enough for the design of diss Python.
as to why Python used def
that year, it didn't really make much sense, just out of Guido's intuition: -) (see python.org/dev/peps/pep-0255/-sharpwhy-not-a-new-keyword-instead-of-reusing-def" rel=" nofollow noreferrer "> PEP 255 ).
I think it's for clarity, distinguishing generators from normal functions in terms of code. Because I can't think of any technical reason why it has to be done.
for the compiler: if you don't add an asterisk, the compiler will be even less efficient from now on, because you can't infer that this is a normal blockStatement when you see function.
to developers, it looks like a function, and I can't get the return value or execute the code automatically. I must be scared to death as a magical programmer like me.
btw I thought there was a pointer in js. It scared me to death.
from the questions you ask to your responses to others, I always feel that you are not asking questions sincerely, but that you have some purpose, but you have not achieved it.