【问题标题】:Python: replacing method in calendar modulePython:日历模块中的替换方法
【发布时间】:2010-10-02 00:13:51
【问题描述】:

我正在尝试替换日历模块中的两种方法:

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 我现在想清楚已经太晚了,我已经围绕这个问题讨论了一个多小时。

提前致谢,
雅库布

【问题讨论】:

    标签: python calendar


    【解决方案1】:

    尝试替换类中的方法而不是实例。

    像这样:

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

    【讨论】:

      【解决方案2】:

      您不想替换方法; 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 教程或网络上的其他地方阅读有关 Inheritance 的更多信息。它是 Python(以及一般的面向对象编程)中的一个重要工具,许多库都是围绕它设计的。

      【讨论】:

      • 现在我知道为什么我的第一个版本不起作用了——我忘记了“日历”。在 HTMLCalendar 之前。感谢Piet您的示例,并感谢大家对我的菜鸟问题的兴趣!祝大家好运
      【解决方案3】:

      已更新,按照 Aaron 在 cmets 中的建议使用 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'
      

      【讨论】:

      • 新模块已弃用。请改用types.MethodType。它的构造函数采用相同的参数和new.instancemethod
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2011-06-24
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多