+ 56

What is Generator In Python

I'm Trying to learn this concept from many days. But didn't got any Specific answer on Google or Sololearn . Can anyone explain me please? What is Generator in Python and for what it's used for ?

8th Dec 2020, 5:40 AM
Kartik
Kartik - avatar
24 odpowiedzi
+ 20
Kartik Taneja Take a look at this code - def generate (str): for s in str: yield s for ch in generate("Hello"): print(ch) Output 》 H e l l o Notice how no variables are used. And also at each iteration one value is returned by the generator. This makes the code much easier. Hope it helps now!
8th Dec 2020, 5:56 AM
Soumik
Soumik - avatar
+ 21
Kartik Taneja Generator is a function which "yields" one value at a time. It is helpful when we need to iterate over objects. It basically returns one value at a time. This is is really useful in many cases. Also it doesn't have to store the complete value before returning it. So it takes up almost no memory. Refer to this site for some in depth explanation - https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/generator
8th Dec 2020, 5:49 AM
Soumik
Soumik - avatar
+ 16
Kartik Taneja though ♨ Soumik ✅ did a good explanation here are some basic examples based on the same resource https://code.sololearn.com/c3z1MS63LxDX/?ref=app
8th Dec 2020, 6:55 AM
BroFar
BroFar - avatar
+ 14
BroFar Thanks sir ! It Really helped !
8th Dec 2020, 1:13 PM
Kartik
Kartik - avatar
+ 13
♨ Soumik ✅ Thanks for your help !!🤗
8th Dec 2020, 1:14 PM
Kartik
Kartik - avatar
+ 9
Rahul Verma Thanks !
8th Dec 2020, 1:14 PM
Kartik
Kartik - avatar
+ 8
Añøńymöüß Thanks di 🤗
8th Dec 2020, 1:14 PM
Kartik
Kartik - avatar
+ 7
 is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
8th Dec 2020, 6:36 AM
Rahul Verma
Rahul Verma - avatar
8th Dec 2020, 3:43 PM
Kartik
Kartik - avatar
+ 5
Norgile BONOU as ♨ Soumik ✅ mentioned and if you look at my code above you will see I did 2 different types of reverses
9th Dec 2020, 4:00 AM
BroFar
BroFar - avatar
+ 4
Generators are a type of iterable, like lists or tuples. Unlike lists, they don't allow indexing with arbitrary indices, but they can still be iterated through with for loops. They can be created using functions and the yield statement.
8th Dec 2020, 3:42 PM
Abhilash Chaurasiya
Abhilash Chaurasiya - avatar
+ 3
generatorss create outputs. i like to use them to make lists def generator_squares (n): for x in range(n): yield x **2 print(list(generator_squares(4))) ——> [0,1,4,9]
9th Dec 2020, 1:01 AM
madeline
madeline - avatar
+ 3
A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
10th Dec 2020, 3:11 AM
Sai Kiran T
Sai Kiran T - avatar
+ 2
How to reverve order in the list?
8th Dec 2020, 10:09 PM
Norgile BONOU
+ 2
Norgile BONOU Suppose a list - myList = [4, 3, 7, 9, 10] Now, to reverse it, use list slicing - myList[::-1] What this does is goes from the starting element of the list to the last element, but in reverse order.
9th Dec 2020, 2:14 AM
Soumik
Soumik - avatar
+ 2
Generator its construction which allow return something and also Generator cant raise memory error. Memory error - error which mean that memory of computer is filled
9th Dec 2020, 7:10 PM
Arman
+ 2
Python generator works by maintaining its own local state, so that if the function called subsequent times then it can resume again exactly where it left. we can say that a generator used to like, a powerful iterator.
9th Dec 2020, 8:15 PM
Arman Gupta
Arman Gupta - avatar
+ 2
hello, Generators are iterators, but you can only iterate over them once. It's because they do not store all the values in memory, they generate the values on the fly. A python generator looks like a function but the yield statement makes the difference. The yield statement turns a function into the generator. When you call a generator function, it will return a generator object but does not start execution immediately. The function execution begins on the very first call to next() method. The execution of the code stops when a yield statement has been reached. Once the yield statement is encountered, the function is paused and control is transfer to the caller with the yield value. As soon as "next" is called again on the generator object, the generator function will resume execution right after the yield statement in the code, where the last call exited. But remember that a function execution always begins from the start of the function body. Let's understand the concept using an example. def abc(): number = 1 print("abc") yield number number = 2 print("xyz") yield number number = 3 print("def") yield number obj = abc() # Return a generator object. Function execution is not started yet next(obj) # Function execution started. # Output: abc # 1 next(obj) # Output: xyz # 2 next(obj) # Output: def # 3 next(obj) # Give error. As nothing to yield. Raise StopIteration exception I hope this will help you
14th Dec 2020, 4:31 AM
Ishan Shah
Ishan Shah - avatar
+ 2
Generators are used to create iterators, but with a different approach. Generators are simple functions that return an iterable set of items, one at a time, in a special way. When an iteration over a set of items starts using the for a statement, the generator is run. Check Web - https://www.tecocraft.co.uk/JUMP_LINK__&&__python__&&__JUMP_LINK-development/
18th Dec 2020, 7:00 AM
Tecocraft LTD
Tecocraft LTD - avatar
+ 2
in my simple thinking a generator is a function that returns an iterator object on which we can iterate (one value at a time)
25th Dec 2020, 4:58 PM
Muhammad Hussein Isron
Muhammad Hussein Isron - avatar