473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

generator with subfunction calling yield

Hi,

I might understand why this does not work, but I am not convinced it
should not - following:

def nnn():
print 'inside'
yield 1

def nn():
def _nn():
print 'inside'
yield 1

print 'before'
_nn()
print 'after'
for i in nnn():
print i

for i in nn():
print i

gives results:

$ python f.py
inside
1
before
after
Traceback (most recent call last):
File "f.py", line 18, in ?
for i in nn():
TypeError: iteration over non-sequence

while I would expect:
$ python f.py
inside
1
before
inside
1
after

Any insight?

andy

Sep 27 '06 #1
3 3493
an**************@gmail.com wrote:
Hi,

I might understand why this does not work, but I am not convinced it
should not - following:

def nnn():
print 'inside'
yield 1

def nn():
def _nn():
print 'inside'
yield 1

print 'before'
_nn()
print 'after'
for i in nnn():
print i

for i in nn():
print i

gives results:

$ python f.py
inside
1
before
after
Traceback (most recent call last):
File "f.py", line 18, in ?
for i in nn():
TypeError: iteration over non-sequence

while I would expect:
$ python f.py
inside
1
before
inside
1
after

Any insight?

andy
In the commented line, you are only creating a generator. This is not
equivalent to calling its "next" function, i.e., nothing will be
"yielded" the way you have written it.
def nn():
def _nn():
print 'inside'
yield 1

print 'before'
_nn() # <== just makes a generator
print 'after'

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Sep 27 '06 #2
wrote in news:11*********************@h48g2000cwc.googlegro ups.com in
comp.lang.python:
Any insight?
From the docs:

<quote>
The yield statement is only used when defining a generator function, and is
only used in the body of the generator function. Using a yield statement in
a function definition is sufficient to cause that definition to create a
generator function instead of a normal function.
</quote>

Note that its when a function defention contains a yeild statement
(expression) that the defenition is taken to be a generator function.
def nn():

def _nn():
print 'inside'
yield 1

print 'before'

_nn()
print 'after'
So tha above (nn()) isn't a generator as it doesn't contain a
yield statement.

Note also that the call to _nn() returns a generator, it isn't a
regular function call.

Here is nn() re-writen to return what you may have originaly expected:

def nn():
def _nn():
print 'inside'
yield 1

print 'before'

for i in _nn():
yield i

print 'after'

So to forward another generator you need to iterate over it and
yield each element, just as you would for any other iterable.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Sep 27 '06 #3
The issue is that nn() does not return an iterable object. _nn()
returns an iterable object but nothing is done with it. Either of the
following should work though:

def nn():
def _nn():
print 'inside'
yield 1
print 'before'
for i in _nn():
yield i
print 'after'

Or you could just return the iterable object that was returned by
_nn():

def nn():
def _nn():
print 'inside'
yield 1
print 'before'
retval = _nn():
print 'after'
return retval

For clarification, using yeild creates a generator. That generator
returns an iterable object. Nesting generators does not somehow
magically throw the values up the stack. I made the same mistake when I
first started messing with generators too.

-Matt

an**************@gmail.com wrote:
Hi,

I might understand why this does not work, but I am not convinced it
should not - following:

def nnn():
print 'inside'
yield 1

def nn():
def _nn():
print 'inside'
yield 1

print 'before'
_nn()
print 'after'
for i in nnn():
print i

for i in nn():
print i

gives results:

$ python f.py
inside
1
before
after
Traceback (most recent call last):
File "f.py", line 18, in ?
for i in nn():
TypeError: iteration over non-sequence

while I would expect:
$ python f.py
inside
1
before
inside
1
after

Any insight?

andy
Sep 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TheDustbustr | last post by:
<code> from __future__ import generators from time import time threads= def sleep(n): print "In Sleep" t=time() while (t+n>time()): yield None
72
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the...
9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
8
by: Paul Chiusano | last post by:
I've been playing around with generators and have run into a difficulty. Suppose I've defined a Node class like so: class Node: def __init__(self, data=None, left=None, right=None):...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
12
by: Thomas Lotze | last post by:
Hi, I'm trying to figure out what is the most pythonic way to interact with a generator. The task I'm trying to accomplish is writing a PDF tokenizer, and I want to implement it as a Python...
5
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
11
by: vbgunz | last post by:
I am afraid that this is the first time in which I would probably need something explained to me as if I were a little child. I am having a hard time getting this through my thick skull. What in...
3
by: Ehsan | last post by:
hi coulde any one show me the usage of "yield" keyword specially in this example: """Fibonacci sequences using generators This program is part of "Dive Into Python", a free Python book for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.