473,407 Members | 2,629 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,407 software developers and data experts.

generators improvement


Here is a simple idea to make generators more useful for some applications,
although I won't for now name them..
anyways, advantage of current generators was not too obvious for all the
people, too.

So, currently the interface between generator and it's user program is rather
poor, because it only consists of a signal from a user routine, and a kind
of only one directional data passage - from the generator.
This seems unsymmetric to me, and thus incomplete..
What I think should increase the potential usefulness of a generators,
is bi-directional data passage. This is very easy to implement, IMHO.

What needs to be done is to allow yield return something - whatever was
passed to the .next() thing from caller..
Easy and obvious, isn't it? So is there any principal problem with this
idea that would prevents its implementation?

I understand that this can be achieved by using some
global, for instance, but hey, I used to implement generator's functionality
without using them, too - just make a class and make
algorithm look horrible. The same thing, but much wordier and uglier
looking... and that does not mean that generators are redundant.

Jul 18 '05 #1
4 1604
Oleg Leschov <ka****@udm.ru> wrote in
news:bh**********@ocasysi.rubbernet.net:
What needs to be done is to allow yield return something - whatever was
passed to the .next() thing from caller..
Easy and obvious, isn't it? So is there any principal problem with this
idea that would prevents its implementation?
Please read PEP 288, http://www.python.org/peps/pep-0288.html
in particular the section:
Rejected Alternative
One idea for passing data into a generator was to pass an argument
through next() and make a assignment using the yield keyword:

datain = yield dataout
. . .
dataout = gen.next(datain)

The intractable problem is that the argument to the first next() call
has to be thrown away, because it doesn't correspond to a yield
keyword.



--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2

Oleg Leschov:
<skip>
that either wouldn't take any arguments or will pass them as generator
function's parameters (which are not allowed at all now, anyway).


Sorry, that was my wrong statement. Of cause these parameters are passed during
creation of generator object.
Still, this does not neglect other variant.

<skip>

Jul 18 '05 #3

"Duncan Booth" <du****@NOSPAMrcp.co.uk> wrote in message
news:Xn***************************@127.0.0.1...
Oleg Leschov <ka****@udm.ru> wrote in
news:bh**********@ocasysi.rubbernet.net:
What needs to be done is to allow yield return something - whatever was
passed to the .next() thing from caller..
Easy and obvious, isn't it? So is there any principal problem with this
idea that would prevents its implementation?
Please read PEP 288, http://www.python.org/peps/pep-0288.html
in particular the section:
Rejected Alternative
One idea for passing data into a generator was to pass an argument
through next() and make a assignment using the yield keyword:

datain = yield dataout
. . .
dataout = gen.next(datain)

The intractable problem is that the argument to the first next() call has to be thrown away, because it doesn't correspond to a yield
keyword.


"I can't figure out how to do it so that it satisfies my sense of esthetics"
is not the same as "this problem doesn't need to be solved."

John Roth

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

Jul 18 '05 #4
Oleg Leschov:
What I think should increase the potential usefulness of a generators,
is bi-directional data passage. This is very easy to implement, IMHO.

What needs to be done is to allow yield return something - whatever was
passed to the .next() thing from caller.. ... I understand that this can be achieved by using some
global, for instance, but hey, I used to implement generator's functionality without using them, too - just make a class and make
algorithm look horrible. The same thing, but much wordier and uglier
looking... and that does not mean that generators are redundant.


While somewhat cumbersome, you could do (mind the bad
cut&paste - OE Express didn't like the tabs PythonWin sent over)

class State:
def __init__(self):
self.args = None
self.kwargs = None

class OlegYield:
def __init__(self, f):
self.f = f
def __call__(self, *args, **kwargs):
state = State()
args = (state,) + args
return OlegIter(state, f(*args, **kwargs))

class OlegIter:
def __init__(self, state, iter):
self.state = state
self.iter = iter
def __iter__(self):
return self
def next(self, *args, **kwargs):
self.state.args = args
self.state.kwargs = kwargs
return self.iter.next()
def f(state, n): .... for i in range(n):
.... yield i
.... print state.args, state.kwargs
.... g = OlegYield(f)
a = g(5)
a.next() 0 a.next(6, a=7) (6,) {'a': 7}
1 list(g(4)) () {}
() {}
() {}
() {}
[0, 1, 2, 3]


Andrew
da***@dalkescientific.com
Jul 18 '05 #5

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

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
6
by: Talin | last post by:
I've been using generators to implement backtracking search for a while now. Unfortunately, my code is large and complex enough (doing unification on math expressions) that its hard to post 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...
13
by: Martin Sand Christensen | last post by:
Hi! First a bit of context. Yesterday I spent a lot of time debugging the following method in a rather slim database abstraction layer we've developed: ,---- | def selectColumn(self,...
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: 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
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...
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
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...
0
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...

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.