Python Forum
Using Generators and their expressions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Using Generators and their expressions (/thread-2454.html)



Using Generators and their expressions - BeerLover - Mar-17-2017

I am trying to get my head around generators. I do not really understand the concept of generators and their expressions, however I read what they are used for and it seems that I might need them going further when learning Python (3.6). 

There is this code. And the printing just won't work: 

(Python 3.6 (spyder3)) 
def simple_generator_function():
    yield 1
    yield 2
    yield 3
    
our_generator = simple_generator_function()
for i in list(our_generator):
    print((i))
Output:
Error:    File "...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile     exec(compile(f.read(), filename, 'exec'), namespace) ...  print((i)) UnsupportedOperation: not writable
Why can not I print this iterable object?  (print(list(i)) doesn't work either)


RE: Using Generators and their expressions - ichabod801 - Mar-18-2017

Works fine for me, prints 1, 2, 3. It must be your Anaconda/Spyder installation. Can you print other things?

p.s. Use python tags (https://python-forum.io/misc.php?action=help&hid=25), I added them for you this time.


RE: Using Generators and their expressions - BeerLover - Mar-18-2017

(Mar-18-2017, 01:21 AM)ichabod801 Wrote: Works fine for me, prints 1, 2, 3. It must be your Anaconda/Spyder installation. Can you print other things?

p.s. Use python tags (https://python-forum.io/misc.php?action=help&hid=25), I added them for you this time.

Thank you for the reply.There was indeed a problem with spyder. Just reinstalled everything and it works fine now.


RE: Using Generators and their expressions - Ofnuts - Mar-18-2017

Pretty good explanation here: https://en.wikipedia.org/wiki/Generator_(computer_programming)