我試圖在Python中創建對象列表。下面的代碼應該(希望)解釋我在做什麼:循環並創建對象數組
class Base(object):
@classmethod
def CallMe(self):
out = []
#another (models) object is initialised here which is iterable
for model in models:
#create new instance of the called class and fill class property
newobj = self.__class__()
newobj.model = model
out.append(newobj)
return out
class Derived(Base):
pass
我想初始化類,如下所示:
objs = Derived.CallMe()
我想「OBJ文件」包含的對象列表我可以迭代。
我得到的堆棧跟蹤以下錯誤:TypeError: type() takes 1 or 3 arguments
在包含newobj = self.__class__()
是否有某種方式來做到這一點,或者線路我在看問題的錯誤的方式?
您的問題和示例並未顯示需要使用類和類方法。有沒有一個,或者你可以使用一個函數作爲工廠來構建你的對象列表?簡單通常更好。 – Petri