Class problems.

S

special_dragonfly

Hello,
I'm having problems retrieving data I think I've put into my program. I have
a class and a function. I'm reading in from a personally made text file of
the data needed for the class. The FieldsDictionary needs to be accesable
outside the function, but my problem is this:
the print FieldsDictionary[key].Fieldname (which I should have just created
because of the line above), returns:
AttributeError: 'list' object has no attribute 'Fieldname'
am I just accessing it wrongly?
I was under the impression that Fields Dictionary should contain a key
referencing to a list of instances of the class. i.e.
FieldsDictionary{key:[instance1, instance2, instance 3]}
Is this not what I've programmed?

class FieldClass(object):
def
__init__(self,Fieldname="",Fieldlength=0,Type=["A","S","N"],Location=["D","C","L","H","TBA"]):
self.Fieldname=Fieldname
self.Fieldlength=Fieldlength
self.Type=Type
self.Location=Location

def
EnterDictionary(FieldsDictionary,key,myfile,FIELD_QUANTITY_OFFSET,LINE_START,LINE_END):
data=myfile.readline().strip()
for i in range(int(data[FIELD_QUANTITY_OFFSET:])):
args =myfile.readline().strip()[LINE_START:LINE_END].split(",")
print args
FieldsDictionary.setdefault(key, []).append(FieldClass(*args))
print FieldsDictionary[key].Fieldname
 
D

Diez B. Roggisch

special_dragonfly said:
Hello,
I'm having problems retrieving data I think I've put into my program. I
have a class and a function. I'm reading in from a personally made text
file of the data needed for the class. The FieldsDictionary needs to be
accesable outside the function, but my problem is this:
the print FieldsDictionary[key].Fieldname (which I should have just
created because of the line above), returns:
AttributeError: 'list' object has no attribute 'Fieldname'
am I just accessing it wrongly?
I was under the impression that Fields Dictionary should contain a key
referencing to a list of instances of the class. i.e.
FieldsDictionary{key:[instance1, instance2, instance 3]}
Is this not what I've programmed?

class FieldClass(object):
def
__init__(self,Fieldname="",Fieldlength=0,Type=["A","S","N"],Location=["D","C","L","H","TBA"]):
self.Fieldname=Fieldname
self.Fieldlength=Fieldlength
self.Type=Type
self.Location=Location

def
EnterDictionary(FieldsDictionary,key,myfile,FIELD_QUANTITY_OFFSET,LINE_START,LINE_END):
data=myfile.readline().strip()
for i in range(int(data[FIELD_QUANTITY_OFFSET:])):
args =myfile.readline().strip()[LINE_START:LINE_END].split(",")
print args
FieldsDictionary.setdefault(key, []).append(FieldClass(*args))

Here you create a list or expect one, and append a new FieldClass-object to
it.
print FieldsDictionary[key].Fieldname

Here you access the LIST, but then expect it to be a FieldClass-object?!?

print FieldsDictionary[key][-1].Fieldname

will work.

Diez
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top