Why isn't my monkey patching isn't working ?
<p>I am trying to add a method to a class that I import.</p>

<p>This is my code : </p>

<pre><code>from pyrser.parsing import node

def to_dxml(self):
    return "test"

node.Node().to_dxml = to_dxml

tree = node.Node()
tree.ls = [1, 2.0, "titi", True, [2, 3, 4, [3, [3, 4]], 5]]
tree.dct = {"g":1, "y":2, "koko":{'D', 'T', 'C'}}
tree.aset = {'Z', 'X', 'T', 'U'}
tree.ablob = b'\xFF\xaa\x06Th -}'
print(tree.to_dxml())
</code></pre>

<p>But when I run it I get</p>

<pre><code>AttributeError: 'Node' object has no attribute 'to_dxml'
</code></pre>

<p>Any idea why it isn't working ? </p>

<p>You need to add attribute to the class, not object.</p>

<pre><code>node.Node().to_dxml = to_dxml
</code></pre>

<p>Should be</p>

<pre><code>node.Node.to_dxml = to_dxml
</code></pre>

python