Class problems.

Discussion in 'Python' started by special_dragonfly, Aug 14, 2007.

  1. 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
    special_dragonfly, Aug 14, 2007
    #1
    1. Advertising

  2. Just ignore this, I was being an idiot...

    "special_dragonfly" <> wrote in message
    news:46c1a34a$0$54289$...
    > 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
    >
    special_dragonfly, Aug 14, 2007
    #2
    1. Advertising

  3. 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
    Diez B. Roggisch, Aug 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyril
    Replies:
    2
    Views:
    3,834
    cyril
    Aug 25, 2004
  2. Purush
    Replies:
    4
    Views:
    1,606
    Purush Rudrakshala
    Apr 13, 2005
  3. E11
    Replies:
    1
    Views:
    4,711
    Thomas Weidenfeller
    Oct 12, 2005
  4. christopher diggins
    Replies:
    16
    Views:
    731
    Pete Becker
    May 4, 2005
  5. Joseph Turian
    Replies:
    5
    Views:
    576
Loading...

Share This Page