扩展 Python Minidom

Extending Python Minidom

有办法扩展 minidom?

我用 ZipFile 试过这个测试,它有效:

文件中有此代码 testzip.py:

import zipfile

class zip( zipfile.ZipFile ):
   def test( self ):
      print( 'test' )

此示例代码运行良好:

import sys
import testzip

zip = testzip.zip( sys.argv[1], 'r' )
zip.test()

我尝试过很多方法来对 minidom 做同样的事情,但没有结果。

基本上,我想要一个 class 具有 minidom 方法和用户定义方法的方法:

import myminidom

doc = myminidom.parseString( txt )
doc.getElementsByTagName("meta")
doc.customMethod1( customArg1 )

有办法得到这个,还是不可能? 我试过 class myminidom( minidom ),我知道这是不可能的,因为我不能扩展一个对象,我试过 class myminidom( minidom.parseStr ),但我得到了 TypeError: function() argument 1 must be code, not str...

minidom 不是 class。它是一个模块,你不能继承模块。 如果你真的想,你可以做一个代理

见: How to proxy all methods from a Python module to another?