Python:替换日历模块中的方法

quia_a 发布于 2018-10-11 calendar 最后更新 2018-10-11 22:58 30 浏览

我试图在日历模块中替换两种方法:

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")
这是行不通的 - 有人可以将我指向相关文献或指出我做错了什么? 我试图从以下线程实施Alan Hynes的建议:thread 现在对我来说思考得太迟了,我一直在解决这个问题一个多小时。 在此先感谢,
的Jakub
已邀请:

dquia

赞同来自:

尝试替换类而不是实例的方法。 喜欢这个:

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")                                                                            

mut

赞同来自:

更新为使用了Aaron在评论中建议的types.MethodType。 尝试:

import types
c.formatday = types.MethodType(ext_formatday, c, calendar.HTMLCalendar)
请参阅types module文档。要了解它失败的原因:
In [53]: class A(object):
   ....:     def foo(self): pass
In [54]: def bar(self): pass
In [55]: a = A()
In [56]: a.foo
Out[56]: <bound method A.foo of <__main__.A object at 0x030D4770>>
In [57]: a.foo = bar
In [58]: a.foo
Out[58]: <function bar at 0x030C3EB0>
In [59]: aa = A()
In [60]: aa.foo.im_class, aa.foo.im_func, aa.foo.im_self
Out[60]:
(<class '__main__.A'>,
 <function foo at 0x030EE6F0>,
 <__main__.A object at 0x030D4910>)
In [61]: a.foo.im_class
AttributeError: 'function' object has no attribute 'im_class'

pullam

赞同来自:

你不想替换这些方法; Alan Hynes建议将子类化为HTMLCalendar:

class MyCustomCalendar(calendar.HTMLCalendar):
def formatday(self, day, weekday, *notes):
        ...
def formatweek(self, theweek, *notes):
        ...
c = MyCustomCalendar(calendar.MONDAY)
这将创建一个新的派生类(MyCustomCalendar),它继承所有HTMLCalendar的方法和属性,但定义了自己的formatdayformatweek版本。 您可以在Python教程或Web上的其他地方阅读有关Inheritance的更多信息。它是Python(以及一般的面向对象编程)中的一个重要工具,并且围绕它设计了许多库。

要回复问题请先登录注册