Python: why didn't you see the method from the superclass?

I am trying to implement my own version of 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) 

but it looks like I missed the fundamental process of subclassing Python ... 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' 

so I need to explicitly add and define other methods such as Write and rotate , for example:

 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 that it would inherit from the base class correctly. Please note that I’m not doing anything, just calling it “super”,

please, can someone explain why I am mistaken in my first idea that there is no need to add the Write method?

Is there a way to tell Python inside my NDailyLogFile that it has all DailyLogFile methods that are not defined directly from its mother class? So that it prevents this error king _sync(klass, klass.__dict__[methodName] and avoids specifying explicitly?

(DailyLogFile source code that inspired me to extract from a curved source https://github.com/tzuryby/freespeech/blob/master/twisted/python/logfile.py )

EDIT: 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' 

therefore will not use it. Mine, although it was right ... I must have missed something.

+7
source share
2 answers

I dare say that twisted/python/threadable.py has an error. __dict__ returns only local attributes, not inherited attributes. Other posts are said to use dir() or inspect.getmembers() to get them.

The good news is that you are right in your first idea that the write method is inherited. The bad news is that Twisted does not recognize inherited methods, so you have to write them yourself.

+2
source

There is one workaround:

 NDailyLogFile.__dict__ = dict( NDailyLogFile.__dict__.items() + DailyLogFile.__dict__.items() ) threadable.synchronize(NDailyLogFile) 

There is a problem here when you use a class without creating it. This workaround works because you force changes to the class attributes before instantiation.

Another important comment ist that for the DailyLogFile subclass, the super command will not work, as DailyLogFile is the so-called “old-style class” or “classobj”. super only works for "new style" classes. See this question for more information on this .

+3
source

All Articles