【问题标题】:Extending Python Minidom扩展 Python Minidom
【发布时间】:2016-01-23 18:18:59
【问题描述】:

有办法扩展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 做同样的事情,但没有结果。

基本上,我想要一个具有 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...

【问题讨论】:

    标签: python dom minidom


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢。你写“如果你真的想要”是因为不推荐使用或不好的做法?
    • 我猜这是一种代码味道,但是,嘿! python 是为成年人准备的 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多