By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 874 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

Interesting little "gotcha" with generators

P: n/a
I recently had need to write the following code:

def compileOuter(self):
if False: yield None
else: return

"compileOuter" is a generator function which is implemented in
various classes. In this particular class, it always yields nothing.
However, none of the following work:

def compileOuter(self):
return

def compileOuter(self):
pass

def compileOuter(self):
yield

The first two don't work because in order to define a generator, you
must have a yield statement inside it. The last doesn't work because
every "yield" must have an argument.

I've been using "return" in generators ever since I started using
generators, but on reflection, it seems to me that such a thing is in
some ways inconsistent; "return" is (conceptually, at least
originally) a function statement, where "return" by itself really
stands in for "return None". But in generators, it is being used as a
control flow command. For example, you can't have "return" with an
argument inside a generator.

Too bad "return" wasn't entirely forbidden within generators, and
"yield" without an argument mandated instead. Oh well, too let now I
suppose...
Cheers,
Ken
Dec 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Kenneth McDonald wrote:
I recently had need to write the following code:

def compileOuter(self):
if False: yield None
else: return

"compileOuter" is a generator function which is implemented in
various classes. In this particular class, it always yields nothing.
However, none of the following work:

def compileOuter(self):
return

def compileOuter(self):
pass

def compileOuter(self):
yield


This would work:

def compileOuter(self):
return iter(())

and depending on how it is used, you might also get away with:

def compileOuter(self):
return []

A generator is simply a function which when called returns an iterator, so
you can use any other function which returns an iterator in its place.
Dec 22 '05 #2

P: n/a
In article <ma***************************************@python. org>,
Kenneth McDonald <ke****************@sbcglobal.net> wrote:

I recently had need to write the following code:

def compileOuter(self):
if False: yield None
else: return


What's wrong with

def foo():
if False: yield None
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Don't listen to schmucks on USENET when making legal decisions. Hire
yourself a competent schmuck." --USENET schmuck (aka Robert Kern)
Dec 22 '05 #3

P: n/a
Kenneth McDonald wrote:
I recently had need to write the following code:

def compileOuter(self):
if False: yield None
else: return

"compileOuter" is a generator function which is implemented in various
classes. In this particular class, it always yields nothing. However,
none of the following work:

def compileOuter(self):
return

def compileOuter(self):
pass

def compileOuter(self):
yield

The first two don't work because in order to define a generator, you
must have a yield statement inside it. The last doesn't work because
every "yield" must have an argument.

I've been using "return" in generators ever since I started using
generators, but on reflection, it seems to me that such a thing is in
some ways inconsistent; "return" is (conceptually, at least originally)
a function statement, where "return" by itself really stands in for
"return None". But in generators, it is being used as a control flow
command. For example, you can't have "return" with an argument inside a
generator.

Too bad "return" wasn't entirely forbidden within generators, and
"yield" without an argument mandated instead. Oh well, too let now I
suppose...


Would this work?

def compilerOuter(self):
raise StopIteration

Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Dec 22 '05 #4

P: n/a
aa**@pythoncraft.com (Aahz) writes:
What's wrong with

def foo():
if False: yield None


Does the following work?

def foo():
raise StopIteration
Dec 22 '05 #5

P: n/a
In article <7x************@ruckus.brouhaha.com>,
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
aa**@pythoncraft.com (Aahz) writes:

What's wrong with

def foo():
if False: yield None


Does the following work?

def foo():
raise StopIteration


Nope.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Don't listen to schmucks on USENET when making legal decisions. Hire
yourself a competent schmuck." --USENET schmuck (aka Robert Kern)
Dec 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.