Python Forum
Using Generators and their expressions
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Generators and their expressions
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
Pretty good explanation here: https://en.wikipedia.org/wiki/Generator_...ogramming)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Statements and Expressions Julie 1 1,591 Feb-26-2021, 05:19 PM
Last Post: nilamo
  Regular Expressions pprod 4 3,017 Nov-13-2020, 07:45 AM
Last Post: pprod
  Regular Expressions amitalable 4 2,710 Mar-14-2019, 04:31 PM
Last Post: DeaD_EyE
  2 iterators or generators Skaperen 1 28,479 Jan-04-2018, 07:41 AM
Last Post: Gribouillis
  Stacking generators Ofnuts 2 19,266 Sep-18-2016, 08:36 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020