0
Do all generators, like the iteration expression in filter(conditional, it. exp. ) do "yield" in the background?
Is this a class, do I look up the namespace, how do I know what Python does with it in the back ground, if it uses a built in function that might return or yield things? If filter is gonna go yield anyway, then is it safe to assume doing a loop with a conditional will be faster? Sorry, some sort of info overload happened to me while I was studying unassumingly, things are getting jumbled, and I am trying to contain it. Thank you.
3 odpowiedzi
+ 4
Korkunç TheTerrible
a site for learning more about Python
https://realpython.com/introduction-to-python-generators/
yield is the keyword used by generators, instead of return in normal functions. The advantage is that the result do not have to be stored in the memory, but is evaluated as needed. It is not about faster, but about memory usage.
+ 2
Bob_Li I was using that site and SO before creating the question. They don't use the term only for def func(x) yield. I see it being mentioned together with some built in functions, and I am having a hard time what exactly they call generator, for instance, the filter itself or the iterable as argument. And I am trying to learn ways to recognize them even if I don't know what's inside the codes of built in functions.
Maybe you'll say they wouldn't have been called functions, then, but generators.
But sometimes a function may have a generator inside and then still return a value because generator isn't the only thing it has.
So then if one doesn't define that func but instead do all it does, because it doesn't need to turn the function into an object, that might be faster.
Thank you for the link though. The lack of replies tells me my question is not clear. I'll sort it.
+ 2
Korkunç TheTerrible
it's about reducing the memory footprint.
https://www.lachlaneagling.com/reducing-memory-consumption-JUMP_LINK__&&__python__&&__JUMP_LINK/amp/
and since you are trying to learn of ways to speed up your program, it is conceivable that you would explore list comprehension next.
Generators vs list comprehension:
https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehensions/
I like to follow strange questions because they often lead me to topics that makes me realize how much I don't know.