srussell
srussell

Reputation: 29

python __str__ in inherited class

The following code to set up __str__ function with an inherited class fails unless there is a "line" between self and the first attribute, i.e. if the line junk = 99, is commented out the program bombs

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)

Thanking you in advance for your help.

Upvotes: 0

Views: 665

Answers (1)

Dragos Pop
Dragos Pop

Reputation: 458

Block(td) sets the self.bidno to td that is a TemplateBlock. From there i suspect that '\n%.2d' %(self.bidno) throws an exception because bidno is not a number.

Upvotes: 1

Related Questions