继承类中的python

2024-09-28 03:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

下面用继承类设置__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)

提前感谢你的帮助。在


Tags: 函数代码selfreturninitdefblocktb
1条回答
网友
1楼 · 发布于 2024-09-28 03:22:47

块(td)设置自我.bidno这是一个模板块。 从那里我怀疑'\n%.2d'%(自我.bidno)引发异常,因为bidno不是数字。在

相关问题 更多 >

    热门问题