interestingly enough in python comparing a 2 classes/closures returned from a method returns False but in Jython its True. ???
 
I find the use case of having a method returning a unique class when it really is the same class in essence, dubious at best.  It took me a long time and looking at the code to see what was happening.  I dont know of other languages that would do this, its an unusual spin on an unusual structure.  I suspect there may be other 'victims' out there just waiting to stumble upon this. :D  I do wonder implementation wise, is it possible to cache the first generated class and just reuse that each time a closure is used?  If so, why not do that?
 
leouser :)


Kent Johnson <kent37@tds.net> wrote:
Leo User wrote:
> Howdy,
>
> ok to the point, in jython-a0 I ran into the strangest behavior when I
> was developing jyleo. I had a method that wrapped a position iterator
> with a java.util.Iterator. This was all fine. But when I wrote out a
> leo file the memory increased by 6%, each time!. I could not fathom
> what was going on and had to instrument the jython implementation to get
> visibility. What I found was somewhat startling, a new class was
> generated each time the method was called.
>
> so a seemingly harmless method/closure like this:
> def example():
> ...class example_closure:
> ......pass
> ...return example_closure()
>
> ended up eating up alot of memory. For methods that get called alot
> this cant be used. For those that called a little its ok. But I must
> ask, does anyone think this is normal behavior? We debated this on the
> Leo forums and Edward Ream pointed out that class is an executable
> statement, hence generating a new class each time was correct. I still
> find myself debating the utility of such a mechanism, I find myself
> saying "Jython Closures Considered Harmful".

I would have to agree with Edward - class is an executable statement. CPython has the same behavior:
>>> def example():
... class example_closure:
... pass
... return example_closure()
...
>>> a=example()
>>> a.__class__

>>> b=example()
>>> b.__class__



If the nested class doesn't actually need the closure then just pull it out of the def:
class example_closure:
pass
def example():
return example_closure()

Alternately you could pass the relevant part of the closure to the example_closure() constructor:
class example_closure:
def __init__(self, state):
self.state = state
def example(state):
return example_closure(state)

Kent



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Jython-dev mailing list
Jython-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-dev


Start your day with Yahoo! - make it your home page