473,473 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

lambda generator - curious behavior in 2.5

>>x = (lambda : ((yield 666),(yield 777),(yield 888)))()
>>x.next()
666
>>x.next()
777
>>x.next()
888
>>x.next()
(None, None, None)
>>x = (lambda : ((yield 666),(yield 777),(yield 888)) and None)()
x.next()
666
>>x.next()
777
>>x.next()
888
>>x.next()
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
x.next()
StopIteration
>>>
Apr 21 '07 #1
3 3004
Boris Borcic wrote:
>>x = (lambda : ((yield 666),(yield 777),(yield 888)))()
>>x.next()
666
>>x.next()
777
>>x.next()
888
>>x.next()
(None, None, None)
>>x = (lambda : ((yield 666),(yield 777),(yield 888)) and None)()
>>x.next()
666
>>x.next()
777
>>x.next()
888
>>x.next()

Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
x.next()
StopIteration
>>>
Seems like a bug:

pydef doit():
((yield 1), (yield 2), (yield 3))
....
pyx = doit()
pyx.next()
1
pyx.next()
2
pyx.next()
3
pyx.next()
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.StopIteration'>

James
Apr 21 '07 #2
En Sat, 21 Apr 2007 06:21:00 -0300, Boris Borcic <bb*****@gmail.com>
escribió:
>>x = (lambda : ((yield 666),(yield 777),(yield 888)))()
>>x.next()
666
>>x.next()
777
>>x.next()
888
>>x.next()
(None, None, None)
I think nobody thought in advance this case (a lambda expression with
yield?), else it would have been forbidden. The lambda is roughly
equivalent to:

def anonymous():
return ((yield 666),(yield 777),(yield 888))
x = anonymous()

but *that* function is forbidden: a generator cannot contain a "return
something" statement. Writing it as a lambda expression, you are bypassing
the compiler check.
Those three None are the 3 yield values, combined into one tuple. You can
verify using send instead of next; the Nones are replaced by the received
values:

pyx = (lambda : ((yield 666),(yield 777),(yield 888)))()
pyx.send(None)
666
pyx.send(1)
777
pyx.send(2)
888
pyx.send(3)
(1, 2, 3)

That was the return in effect. As the function (or lambda) is exited, the
next try should raise StopIteration:

pyx.send(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

Let's check with dis:

pydis.dis(x.gi_frame.f_code)
1 0 LOAD_CONST 0 (666)
3 YIELD_VALUE
4 LOAD_CONST 1 (777)
7 YIELD_VALUE
8 LOAD_CONST 2 (888)
11 YIELD_VALUE
12 BUILD_TUPLE 3
15 RETURN_VALUE
>>x = (lambda : ((yield 666),(yield 777),(yield 888)) and None)()
>>x.next()
666
>>x.next()
777
>>x.next()
888
>>x.next()

Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
x.next()
StopIteration
>>>
This time, the ((tuple) and None) is like saying "discard the tuple and
return None instead", and that fires the usual StopIteration.

--
Gabriel Genellina

Apr 21 '07 #3
[Gabriel Genellina]
This time, the ((tuple) and None) is like saying "discard the tuple and
return None instead", and that fires the usual StopIteration.
It looks like the offending code is in the gen_send_ex() function in
Objects/genobject.c:

if (result == Py_None && f->f_stacktop == NULL) {
Py_DECREF(result);
result = NULL;
/* Set exception if not called by gen_iternext() */
if (arg)
PyErr_SetNone(PyExc_StopIteration);
}

The conditional should probably be:

if (result 1= NULL && f->f_stacktop == NULL)

Please open a bug report on SF and assign to me.
Raymond Hettinger

Apr 21 '07 #4

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

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
7
by: Philip Smith | last post by:
I've read with interest the continuing debate about 'lambda' and its place in Python. Just to say that personally I think its an elegant and useful construct for many types of programming task...
30
by: Mike Meyer | last post by:
I know, lambda bashing (and defending) in the group is one of the most popular ways to avoid writing code. However, while staring at some Oz code, I noticed a feature that would seem to make both...
22
by: Cameron Laird | last post by:
QOTW: "... and to my utter surprise it worked." - Andrew Nagel on his move from wxPython to programming Tkinter in desperation "Python has more web application frameworks than keywords." - Skip...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.