Jython - variables are stored somehow

N

nmin

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.test"]])

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
 
D

Diez B. Roggisch

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.test"]])

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
 
M

Marc 'BlackJack' Rintsch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top