Why does genexp(generator expression) is called genexp? not iterexp?

2024/11/16 4:54:19

A generator is a special kind of iterator, and it has some methods that an normal iterator doesn't have such as send(), close()... etc. One can get a generator by using a genexp like below:

g=(i for i in range(3))

the type of g will be a generator. But it seems weird to me that g is a generator because g.send will do nothing since g isn't returned by a function with yield keyword, and there would be no chance for one to catch the value passed by send method(not sure this is right), I don't see a reason that g needs to be a generator instead of a more generalize type:iterator.

Answer

There seem to be some misunderstanding about definitions:

  1. Iterable is an object that implements __iter__. Also __iter__ is expected to return an iterator.
  2. Iterator is an object that implements next.
  3. Generator function is a function with yield keyword.
  4. Generator object is an object returned by a generator function. Every generator object is an iterator as well.
  5. Generator expression is a generator version of list comprehension. The result of generator expression is also called a generator object or simply a generator.

Some authors use general generator name in one of the meanings above. You have to know the context to truely understand what they are refering to. But since all those objects are closely tied there's no real problem with it.

It's always hard to answer questions "why is it like that?" when asking about naming conventions. Unless you ask the original creators its almost impossible to provide a satisfactory answer. For example it might simply be an effect of evolution and as we all know evolution does introduce errors. But here's my guess:

Both generator expressions and generator functions produce the same type of object. Now why do they produce the same object? That's probably because it was the easiest way to implement it like that under the hood. That might be the motivation.

Also note that there's a noticable difference between iterators and generator expressions: you can use yield keyword in generator expressions. Even though it is not really useful and introduces lots of confusion, the behaviour is not intuitive at all.

Resources:

https://wiki.python.org/moin/Iterator

https://wiki.python.org/moin/Generators

https://en.xdnf.cn/q/119234.html

Related Q&A

why does no picture show

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg if __name__ == "__main__":fig1 = ...print("start plotting")canvas = FigureCanvasQTAgg(fig1)canvas.draw()canvas.show(…

How Normalize Data Mining Min Max from Mysql in Python

This is example of my data in mysql, I use lib flashext.mysql and python 3RT NK NB SU SK P TNI IK IB TARGET 84876 902 1192 2098 3623 169 39 133 1063 94095 79194 …

complex json file to csv in python

I need to convert a complex json file to csv using python, I tried a lot of codes without success, I came here for help,I updated the question, the JSON file is about a million,I need to convert them t…

python pygame - how to create a drag and drop with multiple images?

So Ive been trying to create a jigsaw puzzle using pygame in python.The only problem is that Im having trouble creating the board with multiple images that i can drag along the screen (no need to conne…

Efficiently append an element to each of the lists in a large numpy array

I have a really large numpy of array of lists, and I want to append an element to each of the arrays. I want to avoid using a loop for the sake of performance. The following syntax is not working. a=np…

How to traverse a high-order range in Python? [duplicate]

This question already has answers here:Equivalent Nested Loop Structure with Itertools(2 answers)Closed 4 years ago.In python, we can use range(x) to traverse from 0 to x-1. But what if I want to trave…

How to send eth_requestAccounts to Metamask in PyScript?

I am trying to get address from installed MetaMask on the browser. We used to do this in JS as follow:const T1 = async () => {let Address = await window.ethereum.request({method: "eth_requestAc…

Extract strings that start with ${ and end with }

Im trying to extract the strings from a file that start with ${ and ends with } using Python. I am using the code below to do so, but I dont get the expected result.My input file looks like this:Click …

Weibull distribution and the data in the same figure (with numpy and scipy) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

python: use agg with more than one customized function

I have a data frame like this.mydf = pd.DataFrame({a:[1,1,3,3],b:[np.nan,2,3,6],c:[1,3,3,9]})a b c 0 1 NaN 1 1 1 2.0 3 2 3 3.0 3 3 3 6.0 9I would like to have a resulting dataframe like…