Python:为什么一个方法从超类没有看到? [英] Python : why a method from super class not seen?

查看:422
本文介绍了Python:为什么一个方法从超类没有看到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试实现我自己的版本 DailyLogFile

i am trying to implement my own version of a DailyLogFile

from twisted.python.logfile import DailyLogFile

class NDailyLogFile(DailyLogFile):

     def __init__(self, name, directory, rotateAfterN = 1, defaultMode=None):
         DailyLogFile.__init__(self, name, directory, defaultMode)   # why do not use super. here? lisibility maybe?
         #
         self.rotateAfterN = rotateAfterN

    def shouldRotate(self):
         """Rotate when N days have passed since file creation"""
         delta = datetime.date(*self.toDate()) - datetime.date(*self.toDate(self.createdOn)) 
         return delta > datetime.timedelta(self.rotateAfterN)

    def __getstate__(self):
        state = BaseLogFile.__getstate__(self)
        del state["rotateAfterN"]
        return state

threadable.synchronize(NDailyLogFile)

Python的子类化过程的基础...因为我得到这个错误:

but it looks like i miss a fundamental of Python subclassing process...as i get this error :

Traceback (most recent call last):
  File "/home/twistedtestproxy04.py", line 88, in <module>
    import ndailylogfile
  File "/home/ndailylogfile.py", line 56, in <module>
    threadable.synchronize(NDailyLogFile)
  File "/home/lt/mpv0/lib/python2.6/site-packages/twisted/python/threadable.py", line 71, in synchronize
    sync = _sync(klass, klass.__dict__[methodName])
KeyError: 'write'

所以我需要显式添加和定义像 Write rotate 方法像这样:

so i need to explicitly add and define others methods like Write and rotate method like this:

class NDailyLogFile(DailyLogFile):
     [...]
     def write(self, data): # why must i add these ?
         DailyLogFile.write(self, data)

     def rotate(self): # as we do nothing more than calling the method from the base class!
            DailyLogFile.rotate(self)

threadable.synchronize(NDailyLogFile)

而我认为这将是正确继承从基本的母亲类。注意,我什么都不做,只调用超级,

while i thought it would be correctly inherit from the base mother class. Notice that i do nothing, only calling "super",

请能有人解释为什么我错了我的第一个想法,没有必要添加Write方法?

please can someone explain why i am wrong on my first idea that it was not necessary to add the Write method?

有一种方法对我的NDailyLogFile Python说,它有所有的方法DailyLogFile,没有直接从其母亲类定义?所以,它防止错误 _sync(klass,klass .__ dict __ [methodName] ,并避免指定的隐含的

is there a way to say to Python inside my NDailyLogFile that it shoud have all the methods DailyLogFile that are not defined directly from its mother class? So that it prevents this king of error _sync(klass, klass.__dict__[methodName] and that avoid to specify excplicitly ?

(DailyLogFile的原始代码,启发我从这里扭曲的源代码 https://github.com/tzuryby/freespeech/blob/master/twisted/python/logfile.py

( original code of DailyLogFile that inspired me it taken from the twisted source here https://github.com/tzuryby/freespeech/blob/master/twisted/python/logfile.py )

编辑:关于使用 super ,我获得:

about using super, i get:

  File "/home/lt/inwork/ndailylogfile.py", line 57, in write
    super.write(self, data)
exceptions.AttributeError: type object 'super' has no attribute 'write'

所以不会使用它我的想法是对的...我必须肯定错过了一些

so will not use it. My thoughs was it was right... i must have definitively missed something

推荐答案

我敢说, twisted / python / threadable.py 代码中有一个错误。 code> __ dict __ 只返回本地属性,而不是继承的属性。 其他 帖子说使用 dir() inspect.getmembers code>来获得它们。

I dare say that the twisted/python/threadable.py code has a bug in it. __dict__ only returns the local attributes, not the inherited attributes. Other posts say to use dir() or inspect.getmembers() to get them.

好消息是,你对第一个想法是正确的:方法继承。坏消息是,Twisted不承认继承的方法,所以你必须自己写它们。

The good news is that you are correct on your first idea that the write method is inherited. The bad news is that Twisted doesn't recognize inherited methods, so you have to write them yourself.

这篇关于Python:为什么一个方法从超类没有看到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆