繁体   English   中英

继承类中的python __str__

[英]python __str__ in inherited class

以下代码用继承的类设置__str__函数的代码将失败,除非self和第一个属性之间没有“线”,即,如果junk = 99行被注释掉程序炸弹

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)

预先感谢您的帮助。

Block(td)将self.bidno设置为tD,它是一个TemplateBlock。 从那里我怀疑'\\ n%.2d'%(self.bidno)抛出异常,因为bidno不是数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM