Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It appears that you are not allowed to <a href="http://www.voidspace.org.uk/python/weblog/arch_d7_2009_06_20.shtml" rel="nofollow noreferrer">monkey-patch protocol methods</a> (those that begin and end with double underscores) :</p> <blockquote> <p><strong>Note</strong></p> <p>In practise there is another exception that we haven't handled here. Although you can override methods with instance attributes (very useful for monkey patching methods for test purposes) you can't do this with the Python protocol methods. These are the 'magic methods' whose names begin and end with double underscores. When invoked by the Python interpreter they are looked up directly on the class and not on the instance (however if you look them up directly - e.g. x.<strong>repr</strong> - normal attribute lookup rules apply).</p> </blockquote> <p>That being the case, you may be stuck unless you can go with <a href="https://stackoverflow.com/questions/3391293/different-instance-method-behavior-between-python-2-5-and-2-6/3391350#3391350">~unutbu's answer</a>.</p> <p><strong>EDIT</strong>: Or, you can have the base class <code>__unicode__</code> method search the instance object's dict for a <code>__unicode__</code> attribute. If it's present, then <code>__unicode__</code> is defined on the instance object, and the class method calls the instance method. Otherwise, we fall back to the class definition of <code>__unicode__</code>.</p> <p>I <strong>think</strong> that this could allow your existing subclass code to work without any changes. However, it gets ugly if the derived class wants to invoke the class implementation -- you need to be careful to avoid infinite loops. I haven't implemented such hacks in this example; merely commented about them.</p> <pre><code>import types class Dummy(object): def __unicode__(self): func = self.__dict__.get("__unicode__", None) if func: // WARNING: if func() invokes this __unicode__ method directly, // an infinite loop could result. You may need an ugly hack to guard // against this. (E.g., set a flag on entry / unset the flag on exit, // using a try/finally to protect against exceptions.) return func() return u'one' def two(self): return u'two' d = Dummy() print unicode(d) funcType = type(Dummy.__unicode__) d.__unicode__ = types.MethodType(Dummy.two, d) print unicode(d) print d.__unicode__() </code></pre> <p>Testing with Python 2.6 produces the following output:</p> <pre><code>&gt; python dummy.py one two two </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload