The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
My list is empty.. help
Discuss My list is empty.. help in the Python Programming forum on Dev Shed. My list is empty.. help Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 13th, 2004, 07:43 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
My list is empty.. help
How do i keep the values in a list throught the whole instance of the program. Mine list keeps on emptying because it gets reinitalized in the constructor.
i have a doc / view architecture using wxpython.
|

April 13th, 2004, 09:20 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 84
Time spent in forums: 8 h 7 m
Reputation Power: 10
|
|
|
could you please post some code? atleast the offending parts?
|

April 13th, 2004, 10:37 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Code:
#Boa:Doc:ThermoDoc
from wxPython.wx import *
import ThermoView
import time
def create():
return ThermoDoc()
ID_Timer = wxNewId()
class ThermoDoc(wxEvtHandler):
def __init__(self):
wxEvtHandler.__init__(self)
self.view = ThermoView.create(None, self)
self.stop = True
self.onTimer(None)
self.days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
self.hour = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','16','17','18','19','20', '21','22','23']
self.today = -1
self.now = -1
self.value = -1
self.item = []
self.max = 59
self.min = 0
self.temp = 0
self.maxt = 35
self.mint = 10
self.Timer = wxTimer(self, ID_Timer)
EVT_TIMER(self, ID_Timer, self.onTimer)
self.Timer.Start(2000)
def loadItem(self):
in_file = open("Temp.txt","r")
self.i = 1
while 1:
in_line = in_file.readline()
if len(in_line) == 0:
break
in_line = in_line[:-1]
self.item.append(in_line)
self.i = self.i+1
in_file.close()
def delItem(self,i):
del self.item[i]
self.addAllItems()
def clearFile(self):
out_file = open("Temp.txt","w")
out_file.write('')
out_file.close()
def checkFile(self,i):
in_file = open("Temp.txt","r")
#self.i = 1
i = i[:-2]
while 1:
in_line = in_file.readline()
if len(in_line) == 0:
break
in_line2 = in_line[-3:]
in_line = in_line[:-3]
if i == in_line:
return in_line2
#self.i = self.i+1
in_file.close()
def countItem(self):
return len(self.item)
def getItem(self,i):
return self.item[i]
#----------------------------------------------------------
#Boa:Frame:ThermoView
from wxPython.wx import *
from wxPython.lib.buttons import *
from time import *
def create(parent,doc):
return ThermoView(parent,doc)
[wxID_THERMOVIEW, wxID_THERMOVIEWADDBUTTON, wxID_THERMOVIEWDELBUTTON,
wxID_THERMOVIEWLISTEDITCTRLS, wxID_THERMOVIEWMODESELECT, wxID_THERMOVIEWPROG,
wxID_THERMOVIEWRUN, wxID_THERMOVIEWSPINBUTTON, wxID_THERMOVIEWSPINBUTTON1,
wxID_THERMOVIEWTABBUTTON, wxID_THERMOVIEWTABLABEL, wxID_THERMOVIEWTEXTCTRL,
] = map(lambda _init_ctrls: wxNewId(), range(12))
class ThermoView(wxFrame):
def OnSpinDown(self, event):
n = self.field
p = self.textCtrl.GetStringSelection()
count = self.doc.countItem()
if self.mode == 1:
if self.i<count:
self.textCtrl.SetValue(self.doc.getItem(self.i))
self.i = self.i+1
else:
if self.field == 0:
self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decdays(p))
if self.field == 1:
self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decHour(p))
if self.field == 2:
self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decMin(p))
if self.field == 3:
self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decTemp(p))
self.showSelection()
event.Skip()
these are only some of the functions in each class
It doesn't seem to matter which class i keep the list called item. it always gets reset.
when countItem is called in the view class it gives back an empty list
Last edited by netytan : April 13th, 2004 at 11:08 PM.
|

April 13th, 2004, 11:17 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Which list are we talking about here, and can you give us some idea of whats going on. Of course if your creating new instances of your class then the list will not persist since the values are local to each instance, and not a global used by the class. This what you trying to do?
Mark.
P.S. please use CODE tags, its what they are there for. Check out the stickies at the top for more info on posting 
__________________
programming language development: www.netytan.com – Hula
|

April 14th, 2004, 03:30 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
As Netytan says - make your list a global list (declare it in the module outside of a function or class).
When you assign data to the the list from within a function or class method then you should use the global command to make sure python knows you a referring to an item declared in the global (module) namespace.
If you are just modifying the list contents using methods or indexing then you don't need the global command:
Code:
mylist = []
class aclass:
def amethod(self):
global mylist
mylist= [1,2,3,4,"Hello World"]
def afunc():
print mylist
mylist.append("Added by afunc")
class bclass:
def bmethod(self):
mylist[0]= "modified by bclass.bmethod"
print mylist
a = aclass()
a.amethod()
print mylist
afunc()
print mylist
b = bclass()
b.bmethod()
print mylist
To understand more - read up on Namespaces and garbage collection.
Grim 
|

April 14th, 2004, 07:47 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
Alternatively you could define the list at class level, by defining it outside of any class methods. This will make the list common across all the instances of the class. You can then access the list through self.item or ThermoDoc.item.
For example:
Code:
>>> class Test:
... items = []
... def __init__(self, value):
... self.items.append(value)
... def add(self, value):
... self.items.append(value)
... def show(self):
... print self.items
...
>>> t1 = Test('foo')
>>> t1.show()
['foo']
>>> t2 = Test('bar')
>>> t1.show()
['foo', 'bar']
>>> t2.show()
['foo', 'bar']
>>> t2.add(23)
>>> t1.show()
['foo', 'bar', 23]
>>> Test.items
['foo', 'bar', 23]
>>>
When using class-level attributes you have to be careful about assigning to it, since it will create a new attribute bound to the instance and not change the class attribute. e.g.
Code:
>>> t1.items = []
>>> t1.show()
[]
>>> t2.show()
['foo', 'bar', 23]
>>>
>>> Test.items
['foo', 'bar', 23]
Sometimes this is what you want - the class attribute holds a default value which can be overriden in specific instances.
Dave - The Developers' Coach
|

April 14th, 2004, 08:03 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks!
Thanks for the ideas! I will try the global variable. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|