开发者

Problem building a list of class objects in python

开发者 https://www.devze.com 2023-01-14 07:49 出处:网络
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.

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:

TagData()

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:

def addVal(self,value):
    self.val.append(value)

And then:

a=TagData()
a.addVal('value')
tagList.append(a)

Or you could do this (if you don't wish to define getters/setters):

a=TagData()
a.val.append('text')
tagList.append(a)

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.


[] declares a list, so your member variables tag, val, rel etc are all list values, but it sounds like they really want to be scalars? So try this in your constructor:

def __init__(self):
    self.tag = None
    self.val = None
    # etc

Then you'd do something like this to create your objects:

td = TagData()
td.tag = tagvalue
td.val = 'foo'
tagList.append(td)

If you want to be able to specify tag and val in the constructor call, you need to add constructor arguments. Here's one way:

def __init__(self, tag=None, val=None):
    self.tag = tag
    self.val = val
    # etc

Then:

tagList.append(TagData(tag=tagvalue))
tagList.append(TagData(val='foo'))


The immediate problem is that you are not instantiating TagData. The attributes inside __init__ are instance attributes (note the self. prefix used to set them for an instance) and not class attributes. Try the same code with an instance of TagData created thus: TagData()

The broader problem is that I don't exactly understand what you are trying to do. Without that it is hard to answer your question better. Adding more context to your question would help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号