私はこのコードのための適切な解決策を探しています。 XMLをまだ印刷しているうちに、それを印刷することができます。Pythonの動的な上書き魔法の方法
from xml.dom.minidom import Document, DOMImplementation
class MyClass(object):
init():
pass
def create_xml():
doc = Document()
# I know i cannot do that, I know. I need proper solution for that
doc.__str__ = self.print_doc
def print_doc(document):
return document.toprettyxml(encoding='UTF-8')
私たちは、あなたがここに私の考えを見ることができるので、それのためにきれいではない方法で、作品を把握することができます:
from xml.dom.minidom import Document, DOMImplementation
def create_xml():
doc = Document()
document.__str__ = partial(self.print_doc, document=document)
def print_doc(document):
return document.toprettyxml(encoding='UTF-8')
ソフトウェアを扱うことができないとして私のクラスには、静的である必要があります実行中に1つ以上のインスタンスがある場合でも、ユーザーはその実行中に複数のXMLを作成する必要があります(はい、これはうんざりですが、助けません)。
class Child(Document):
def __str__(self):
return document.toprettyxml(encoding='UTF-8')
...ので、私は持っている必要がありbeacauseこのクラスは、すべてのこと...私は
class MyClass(object):
def create_xml():
return Document()
を印刷することができます: は、代わりに変なものを作り、私は私のために動作しますSMTHをしました代わりに
def main():
xml = MyClass.create()
print(xml)
...ちょうど
xml = Document()
申し訳ありませんがそれをオーバーライドし、混乱のために誰もが...私はここに滑走することは、私が最初に
:
また、ラッパークラスを作成することができます。 'doc .__ str__ = print_doc'を試してください。 – jonrsharpe
実際には動作しないが、それ以外のコードでは人にやさしくないとは言わないため、動作しているコードはユーザーフレンドリーではありません。 – Goyo