Python Forum

Full Version: Using Generators and their expressions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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.
(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.