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

Jython - variables are stored somehow

P: n/a
Hi,

I'm using Jython in combination with java.

I wrote a jython skript, which calls a function from another jython
module called library.py.

So, executing the function genData() in skript .py runs without
problem but if I execute the same function again, the data from the
first run is stored somehow and is added to the new data.

So, if you look at the result:
#1 in DatenTypen.py return an empty list each time the program runs.
Ok ... clear so far
#2 in library.py returns an empty list, when the program runs for the
first time ... but when the function is
called again, the list contains an element. Each time you call the
function again, one element is added!
Why?? out.abschnitte should be the same as printed in #1 or not?

Here is the code:

skript.py
======
from library import *

def genData():
out=DMS_sendFiles_ein_Abschnitt([["testdata1.test","testdata2.test","testdata3.t est"]])

return out

library.py
=======
from DatenTypen import AusgangsDatenDeichMonitor
from DatenTypen import DMS_Abschnitt
from DatenTypen import DMS_GeoData
from DatenTypen import DMS_GeoDataFile

def DMS_sendFiles_ein_Abschnitt(filelist):

out=AusgangsDatenDeichMonitor()

print "out.abschnitte: "+str(out.abschnitte) #2

abschnitt=DMS_Abschnitt()

for f in filelist:
data=DMS_GeoData()

for layer in f:

datalayer=DMS_GeoDataFile()

datalayer.dateiname=layer

datalayer.dateiinhalt="TEST"

data.layerFiles.append(datalayer)

abschnitt.bildSequenze.append(data)

out.abschnitte.append(abschnitt)

return out

DatenTypen.py
===========

class AusgangsDatenDeichMonitor:

abschnitte=[]

def __init__(self):
abschnitte=[]
print "Abschnitt in DatenTypen: "+str(abschnitte) #1

class DMS_Abschnitt:

bildSequenze=[]

def __init__(self):
abschnittsNummer=0
bildSequenze=[]

class DMS_GeoData:

layerFiles=[]

def __init__(self):
layerFiles=[]

class DMS_GeoDataFile:

dateiinhalt="dateiinhalt"

dateiname="dateiname"

zipped=False

def __init__(self):
dateiinhalt="dateiinhalt"
dateiname="dateiname"
zipped=False

So, I read about deleting Instances with "del" ... but it does not
work at all.

Any Ideas?

Thanks in advance

Simon

Aug 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
nm**@freenet.de schrieb:
Hi,

I'm using Jython in combination with java.

I wrote a jython skript, which calls a function from another jython
module called library.py.

So, executing the function genData() in skript .py runs without
problem but if I execute the same function again, the data from the
first run is stored somehow and is added to the new data.

So, if you look at the result:
#1 in DatenTypen.py return an empty list each time the program runs.
Ok ... clear so far
#2 in library.py returns an empty list, when the program runs for the
first time ... but when the function is
called again, the list contains an element. Each time you call the
function again, one element is added!
Why?? out.abschnitte should be the same as printed in #1 or not?

Here is the code:

skript.py
======
from library import *

def genData():
out=DMS_sendFiles_ein_Abschnitt([["testdata1.test","testdata2.test","testdata3.t est"]])

return out

library.py
=======
from DatenTypen import AusgangsDatenDeichMonitor
from DatenTypen import DMS_Abschnitt
from DatenTypen import DMS_GeoData
from DatenTypen import DMS_GeoDataFile

def DMS_sendFiles_ein_Abschnitt(filelist):

out=AusgangsDatenDeichMonitor()

print "out.abschnitte: "+str(out.abschnitte) #2

abschnitt=DMS_Abschnitt()

for f in filelist:
data=DMS_GeoData()

for layer in f:

datalayer=DMS_GeoDataFile()

datalayer.dateiname=layer

datalayer.dateiinhalt="TEST"

data.layerFiles.append(datalayer)

abschnitt.bildSequenze.append(data)

out.abschnitte.append(abschnitt)

return out

DatenTypen.py
===========

class AusgangsDatenDeichMonitor:

abschnitte=[]

def __init__(self):
abschnitte=[]
print "Abschnitt in DatenTypen: "+str(abschnitte) #1

class DMS_Abschnitt:

bildSequenze=[]

def __init__(self):
abschnittsNummer=0
bildSequenze=[]

class DMS_GeoData:

layerFiles=[]

def __init__(self):
layerFiles=[]

class DMS_GeoDataFile:

dateiinhalt="dateiinhalt"

dateiname="dateiname"

zipped=False

def __init__(self):
dateiinhalt="dateiinhalt"
dateiname="dateiname"
zipped=False

So, I read about deleting Instances with "del" ... but it does not
work at all.

Any Ideas?
I think you should read a python-tutorial. The above code looks as if
you believe that

class Foo:
name = value

def __init__(self):
name = other_value

will create a class Foo, which then has instances with the property
"name", and that this is bound to other_value. Python isn't doing that.
name in the above example (and e.g. abschnitte in yours) are
class-attributes. That means that ALL instances of Foo share that name!!!

What you have to do is this:

class Foo:
def __init__(self, other_value):
self.name = other_value
please note the self in front of name!

Or, within your example:

class AusgangsDatenDeichMonitor:

def __init__(self):
self.abschnitte=[]
print "Abschnitt in DatenTypen: "+str(abschnitte) #1
There are a great many tutorials for python + OO out there - go read one
(or several).

Regards,

Diez
Aug 9 '07 #2

P: n/a
On Thu, 09 Aug 2007 05:30:00 -0700, nmin wrote:
So, executing the function genData() in skript .py runs without
problem but if I execute the same function again, the data from the
first run is stored somehow and is added to the new data.

So, if you look at the result:
#1 in DatenTypen.py return an empty list each time the program runs.
Ok ... clear so far
#2 in library.py returns an empty list, when the program runs for the
first time ... but when the function is
called again, the list contains an element. Each time you call the
function again, one element is added!
Why?? out.abschnitte should be the same as printed in #1 or not?
`out.abschnitte` is a *class* attribute so it is the same list on all
instances of that class.
class AusgangsDatenDeichMonitor:

abschnitte=[]
Everything on this level belongs to the class, so `abschnitte` is a class
attribute and shared by all instances of `AusgangsDatenDeichMonitor`.
def __init__(self):
abschnitte=[]
print "Abschnitt in DatenTypen: "+str(abschnitte) #1
Remove the class attribute and change the `__init__()` to:

def __init__(self):
self.abschnitte = list()
print "Abschnitt in DatenTypen: " + str(self.abschnitte)

So, I read about deleting Instances with "del" ... but it does not
work at all.
You can't delete objects with ``del``, just names or references to objects
in containers.

Ciao,
Marc 'BlackJack' Rintsch
Aug 9 '07 #3

P: n/a
works perfect ... thanks a lot!

Aug 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.