2016-12-01 177 views
-1

以下用于设置__str__函数和继承类的代码失败,除非在自我和第一个属性之间存在“行”,即如果行junk = 99,则被程序炸弹注释掉python __str__继承类

class TemplateBlock(object) : 

    def __init__(self,        
       tidno  = 1):     
     self.tidno   = tidno    

    def __str__(self) : 
     return '%.2d:' % (self.tidno) 

class Block(TemplateBlock) : 

    def __init__(self,        
       junk  = 99,     
       bidno  = 3) : 
     TemplateBlock.__init__(self)        
     #self.junk   = junk   
     self.bidno   = bidno 

    def __str__(self) : 
     return TemplateBlock.__str__(self) + '\n%.2d' %(self.bidno) 

    def set(self,t) :        
     self.tidno   = t.tidno    

tb = TemplateBlock(tidno=2) 
b = Block(tb) 
b.set(tb) 
print(b) 

感谢您提前寻求帮助。

+0

你能在你的问题中包含回溯吗?说炸弹不够描述。 –

+0

炸弹的含义?请提供您看到的例外和追溯。 – ShadowRanger

+0

附注:你将'tb'传递给'Block',这没有意义;它变成了'junk'或'bidno'参数,它们都不应该是'TemplateBlock'。并且你的超类初始化方法调用不会传递'tidno'的值,当你可以定义子类接受并恰当地传递'tidno'时,'tidno'需要这个愚蠢的'.set(tb)'调用。 – ShadowRanger

回答

0

Block(td)将self.bidno设置为模板块的td。 从那里我怀疑'\ n%.2d'%(self.bidno)会抛出异常,因为bidno不是数字。