I’m still new to python, and been stuck playing around with this for a while and would appreciate someone pointing out where I’m going wrong.
Basically I am trying to build a list that contains a number of different objects, with each object having several attributes.
My attempt is simplified and shown below, basically I built a class for this object, containing several values, and then tried to build a list to contain the objects – it doesn’t seem to be heading anywhere successful!
Any suggestions would be greatly appreciated.
class TagData(object):
def __init__(self):
self.tag = []
self.val = []
self.rel = []
self.database = []
self.description = []
tagvalue='xxx'
tagList=[]
tagList.append(TagData.tag(tagvalue))
tagList.append(TagData.val(val1))
Currently I am getting an error saying TagData has no attribute ‘tag’
Thanks,
First, you need to create an instance of TagData, like this:
This is why you are getting the AttributeError, because when you use TagData.tag(…), you are really trying to call the tag method of the TagData class object, instead of setting a property of a specific instance.
Next, you need to assign to a property or create a method to set it. For example, you could add a method like this to your class:
And then:
Or you could do this (if you don’t wish to define getters/setters):
Do not do all of that on one line, because when you call addVal, it will return None – and then you will append None to your list instead of your object.