我们有三个类BlockTag,RowTag和ColumnTag.RowTag和ColumnTag都从BlockTag扩展。你可以在下面看到它们:
BlockTag:
class BlockTag(object):
def __init__(self,font=None,content="",border=None,right_border=None,left_border=None,top_border=None,bottom_border=None,text_alignment=None,forecolor=None,backcolor=None):
self.content=content
self.border=border
self.right_border=right_border
self.left_border=left_border
self.top_border=top_border
self.bottom_border=bottom_border
self.text_alignment=text_alignment
self.forecolor=forecolor
self.backcolor=backcolor
self.font=font
def __getitem__(self, item):
try:
return getattr(self,item)
except AttributeError:
return None;
RowTag:
class RowTag(BlockTag):
def __init__(self,font=None,content="",border=None,right_border=None,left_border=None,top_border=None,bottom_border=None,text_alignment=None,forecolor=None,backcolor=None,skipInHTML=None,skipInPDF=None,skipInExcel=None,inner_td_tags=None):
super(RowTag,self).__init__(font=font,content=content,border=border,right_border=right_border,left_border=left_border,top_border=top_border,bottom_border=bottom_border,text_alignment=text_alignment,forecolor=forecolor,backcolor=backcolor,)
self.inner_td_tags=inner_td_tags #if inner_td_tags else []
self.skipInExcel=skipInExcel
self.skipInHTML=skipInHTML
self.skipInPDF=skipInPDF
def __iter__(self):
return iter(self.inner_td_tags);
def add_td_tags(self,*input_td_tags):
for i in input_td_tags:
self.inner_td_tags.append(i)
return self;
def __getitem__(self, item):
try:
return getattr(self,item)
except AttributeError:
return None;
ColumnTag:
class ColumnTag(BlockTag):
def __init__(self,font=None,content="",border=None,right_border=None,left_border=None,top_border=None,bottom_border=None,text_alignment=None,forecolor=None,backcolor=None,image=None,row_span=None,col_span=None,width=None,verticalText=None,verticalAlign=None):
super(ColumnTag,self).__init__(font=font,content=content,border=border,right_border=right_border,left_border=left_border,top_border=top_border,bottom_border=bottom_border,text_alignment=text_alignment,forecolor=forecolor,backcolor=backcolor)
self.image=image
self.row_span=row_span
self.col_span=col_span
self.width=width
self.verticalText=verticalText;
self.verticalAlign=verticalAlign;
def __getitem__(self, item):
try:
return getattr(self,item)
except AttributeError:
return None;
问题在于,当我创建两个RowTag类型的对象时,第二个对象具有第一个Object的列!(因为我们知道对象应该是独立的)
r1=RowTag().add_td_tags(
ColumnTag(),
ColumnTag(),
ColumnTag(),
)
r2=RowTag()
print("size=>")
print(len(r2.inner_td_tags))#prints 3!!!!
如果你能帮我一点,我会很感激。
答案 0 :(得分:0)
在rowtag类中保持
self.inner_td_tags=inner_td_tags if inner_td_tags else []
还有一件事,你的python代码中有很多;
。没有必要给它们删除它,你的代码工作正常
print("size=>")
print(len(r2.inner_td_tags))
print("size=>")
print(len(r1.inner_td_tags))
输出
size=>
0
size=>
3