Python: replacing method in calendar module
Question: I'm trying to replace two methods in calendar module:

    
    
    import calendar
    
    
    c = calendar.HTMLCalendar(calendar.MONDAY)
    def ext_formatday(self, day, weekday, *notes):
        if day == 0:
            return '<td class="noday">&nbsp;</td>'
        if len(notes) == 0:
            return '<td class="%s">%d<br /></td>' % (self.cssclasses[weekday], day)
        else:
            return '<td class="%s">%d<br />%s</td>' % (self.cssclasses[weekday], day, notes)
    
    def ext_formatweek(self, theweek, *notes):
        if len(notes) == 0:
            s = ''.join(self.formatday(d, wd) for (d, wd) in theweek)
        else:
            s = ''.join(self.formatday(d, wd, notes) for (d, wd) in theweek)
        return '<tr>%s</tr>' % s 
    
    c.formatday = ext_formatday
    c.formatweek = ext_formatweek
    
    print c.formatmonth(2012,1,"foobar")
    

This won't work - could somebody point me to relevant literature or point out
what I'm doing wrong? I'm trying to implement Alan Hynes suggestion from the
following thread: [thread](http://stackoverflow.com/questions/1101524/python-
calendar-htmlcalendar/1458077#1458077) It way too late for me to think
straight and I've been dancing around that problem for over an hour.

Thanks in advance,  
Jakub


Answer: Try replacing the method at the class instead of the instance.

Like this:

    
    
    import calendar                                                                                                 
    
    
    def ext_formatday(self, day, weekday, *notes):                                                                  
        if day == 0:                                                                                                
            return '<td class="noday">&nbsp;</td>'                                                                  
        if len(notes) == 0:                                                                                         
            return '<td class="%s">%d<br /></td>' % (self.cssclasses[weekday], day)                                 
        else:                                                                                                       
            return '<td class="%s">%d<br />%s</td>' % (self.cssclasses[weekday], day, notes)                        
    
    def ext_formatweek(self, theweek, *notes):                                                                      
        if len(notes) == 0:                                                                                         
            s = ''.join(self.formatday(d, wd) for (d, wd) in theweek)                                               
        else:                                                                                                       
            s = ''.join(self.formatday(d, wd, notes) for (d, wd) in theweek)                                        
        return '<tr>%s</tr>' % s                                                                                    
    
    calendar.HTMLCalendar.formatday = ext_formatday                                                                 
    calendar.HTMLCalendar.formatweek = ext_formatweek                                                               
    
    c = calendar.HTMLCalendar(calendar.MONDAY)                                                                      
    print c.formatmonth(2012,1,"foobar")                                                                            
    

