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

Python generators in Java?

P: n/a
Preamble:

- I know this is the Python forum
- I know about (and have used) Jython

I already posted this question in comp.lang.java. But after a week I have
still not received a single reply.

One of my favorite Python features is generators. I have to use Java for
one particular project (it happens). I would like to have something at
least close to Python's generators for the project.

Jython won't do because last I checked it was dormant, and has been for
quite some time. In addition, the version that it is "frozen" at (2.1)
does not contain generators (if this is wrong on any point, *please* point
me to a URL for a current version). I checked the mailing list archive and
except for a single recent post from a party external to the Jython project,
there hasn't been any mailing list activity since 2002. In fact, I saw my
two unanswered posts about Jython and generators from 2002. There was a
2.2a (alpha release) in July of 2003 with lots of warnings about using the
code in a production environment, but nothing since.

So does anyone here that has Python and Java experience have any ideas on
this?

Also, has anybody had any experience integrating CPython with Java, if it is
at all possible?

Thanks.

--
Robert Oschler
http://www.robotsrule.com/phpBB2/
Robot & Android Discussion Forum
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Robert Oschler wrote:
Preamble:
Jython won't do because last I checked it was dormant, and has been for
quite some time. In addition, the version that it is "frozen" at (2.1)
does not contain generators (if this is wrong on any point, *please* point
me to a URL for a current version). Yes, that is still the current production release.

I checked the mailing list archive and except for a single recent post from a party external to the Jython project,
there hasn't been any mailing list activity since 2002.
??? There is continuous, moderate activity on the ML. Try
http://sourceforge.net/mailarchive/f...?forum_id=5586

In fact, I saw my two unanswered posts about Jython and generators from 2002. There was a
2.2a (alpha release) in July of 2003 with lots of warnings about using the
code in a production environment, but nothing since.


There has been some encouraging news about a Jython update in the works.
See the news at http://www.jython.org
We won't have generators in Jython soon but there is progress.

Kent
Jul 18 '05 #2

P: n/a
[Robert Oschler]
Preamble:

- I know this is the Python forum
- I know about (and have used) Jython

I already posted this question in comp.lang.java. But after a week I
have still not received a single reply.

One of my favorite Python features is generators. I have to use Java
for one particular project (it happens). I would like to have
something at least close to Python's generators for the project.

Hi Robert,

Hmm, I don't see a direct question in your post, so I'll just assume
that you're generally inquiring about generator style functionality in java.

If you're looking to achieve the execution efficiency of python
generators, e.g. in terms of processing sequences one value at a time
rather than generating the entire sequence and then processing, then you
should look at java iterators, which are designed specifically for this
purpose.

Note that java 1.5 brings new syntactic features which support
iterators, e.g. short-hand for loops like this

public int sumArray(int array[]) {
int sum = 0;
for(int i : array) {
sum += i;
}
return sum;
}

Quite pythonic, IMHO.

http://java.sun.com/developer/techni...2se15langfeat/

However, if you're looking for the resumable-functions aspect of
generators, you're out of luck: java doesn't not support them. But note
that a java iterator will still very likely be more efficient than a
python generator, albeit that the code won't be as clean.

Note that there is a currently a proposal from the codehaus people to
add continuations to java, which could make both generators and full
coroutines possible in java.

http://docs.codehaus.org/display/continuation/Home

Lastly, it would probably be easier to discuss this subject if you gave
an example of the type of thing you want to do, and some reasons why you
want to do it that way. Post some python: maybe the java would be
straightforward.

Regards,

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
Jul 18 '05 #3

P: n/a
Robert Oschler wrote:
One of my favorite Python features is generators. I have to use Java for
one particular project (it happens). I would like to have something at
least close to Python's generators for the project.


Sketch:
- create a Queue class
- the 'generator' would run in a thread and put objects into the queue
(equivalent to yield)
- the 'main' program would run in the main thread and consume objects
from the queue (equivalent to calling generator.next ())
- the consumer must block if the queue is empty
- the producer must block if the number of objects in the queue exceeds
a certain amount (queue size = 1 is perfectly acceptable unless the
producer is I/O bound)
- if you use generators to walk recursive structures, then you probably
need only one queue instead of one generator for each level.
- perhaps you can get away with one superclass that creates both queue
and thread, the 'generator' could then be implemented as an anonymous class.

Daniel
Jul 18 '05 #4

P: n/a

"Alan Kennedy" <al****@hotmail.com> wrote in message
news:I%*******************@news.indigo.ie...

Hi Robert,

Hmm, I don't see a direct question in your post, so I'll just assume
that you're generally inquiring about generator style functionality in java.
If you're looking to achieve the execution efficiency of python
generators, e.g. in terms of processing sequences one value at a time
rather than generating the entire sequence and then processing, then you
should look at java iterators, which are designed specifically for this
purpose.

Note that java 1.5 brings new syntactic features which support
iterators, e.g. short-hand for loops like this

public int sumArray(int array[]) {
int sum = 0;
for(int i : array) {
sum += i;
}
return sum;
}

Quite pythonic, IMHO.

http://java.sun.com/developer/techni...2se15langfeat/

However, if you're looking for the resumable-functions aspect of
generators, you're out of luck: java doesn't not support them. But note
that a java iterator will still very likely be more efficient than a
python generator, albeit that the code won't be as clean.

Note that there is a currently a proposal from the codehaus people to
add continuations to java, which could make both generators and full
coroutines possible in java.

http://docs.codehaus.org/display/continuation/Home

Lastly, it would probably be easier to discuss this subject if you gave
an example of the type of thing you want to do, and some reasons why you
want to do it that way. Post some python: maybe the java would be
straightforward.

Regards,

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan


Alan,

Yes it's the resumable aspect of generators I'm after. For me that is their
main appeal. I'm familiar with iterators from C++, but it's the ability to h
ave a function that maintains it's own "state" (generator) that is
attractive. It simplifies a lot of thorny programming problems.

Thanks for the link on Java continuations, I'll check that out.

--
Robert Oschler
http://www.robotsrule.com/phpBB2/
Robot & Android Discussion Forum
Jul 18 '05 #5

P: n/a
On Mon, 15 Nov 2004 16:44:46 +0000, rumours say that Alan Kennedy
<al****@hotmail.com> might have written:
http://java.sun.com/developer/techni...2se15langfeat/


In that page, under "Autoboxing/Unboxing", there is the following
sentence:

"""
In a pure object-oriented language there should be no difference between
a primitive data type and a reference, as everything is an object.
"""

How sweet... a Sun document for Java that can be used for Python
advocacy: "Python is a pure object-oriented language" :)
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.