Jython - variables are stored somehow

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Aug 9 09:11:31 EDT 2007


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



More information about the Python-list mailing list