Class problems.

Diez B. Roggisch deets at nospam.web.de
Tue Aug 14 15:23:36 CEST 2007


special_dragonfly wrote:

> 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



More information about the Python-list mailing list