473,378 Members | 1,420 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,378 software developers and data experts.

Interesting little "gotcha" with generators

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
5 1179
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Warren Oates | last post by:
I ran into an interesting gotcha with unix timestamps. I've got a page where the user inputs the date with drop-down boxes (easy to deal with), that my script sees as (say) $d $m $y, not...
2
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
17
by: Tim Baur | last post by:
Hi All, I'm a VB6er who's recently started using VB2005; I have a general question about variable declaration. In VB, I've always tried to avoid using the "As New" construct when declaring a...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
2
by: sloan | last post by:
VB.NET object "bloating" and WCF. //quote from web page This can result in the following undesirable situations: a.. You end up serializing objects that you didn't expect, resulting in a...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: Just_a_fan | last post by:
Some folks have searched, from time to time, for a dual axis MSChart with different scales on the two Y axes. The sample, extracted from running code I wrote, produces a graph with MSChart (VB9)...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.