我想在 python 中定义相同类型的对象数组(我定义的类)。
我尝试使用数组模块:
from array import *
arrayIdentifierName = array("ClassName",[ClassName1,ClassName2,ClassName3])
它说:
TypeError: array() argument 1 must be char, not str
然后我尝试使用列表: 使用列表不会有帮助,因为我在 http://docs.python.org/2/tutorial/datastructures.html 中没有看到方法可以到达每个对象而不将其从列表中删除(例如:list.pop([i])或list.remove(x)将删除该对象,我需要更改它的数据成员之一并保存它)。
大家有什么建议吗?
谢谢
最佳答案
你应该使用列表......
您可以使用列表访问任何对象,并使用其函数执行您想做的任何操作。
class test(object):
def __init__(self,name):
self.name=name
def show(self):
print self.name
ob1=test('object 1')
ob2=test('object 2')
l=[]
l.append(ob1)
l.append(ob2)
l[0].show()
l[1].show()
关于python - Python 中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20383364/