我想要一个Derived
对象也从Base
对象“继承”数据-这将如何实现
#!python3
#coding=utf-8
class Base:
def __init__(self, attrib):
self.attrib = attrib
listOfBaseObjects = [
Base("this"),
Base("that"),
]
print(listOfBaseObjects)
import copy
class Derived(Base): # ?
def __init__( self, baseObject, # ?
otherattrib):
#Base.__init__(baseObject) # ?
#self = copy.copy(baseObject) # ?
self.otherattrib = otherattrib
def __repr__(self):
return "<Derived: {} {}>".format(self.attrib, self.otherattrib)
listOfDerivedObjects = [
Derived(listOfBaseObjects[0], "this"),
Derived(listOfBaseObjects[1], "that"),
]
print(listOfDerivedObjects)
# AttributeError: 'Derived' object has no attribute 'attrib'
这似乎不是“inherit”的问题,您只想合并来自另一个对象的数据
相关问题 更多 >
编程相关推荐