Jython - variables are stored somehow

Diez B. Roggisch deets at nospam.web.de
Thu Aug 9 09:03:13 EDT 2007


nmin at 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.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



More information about the Python-list mailing list