replacing instance __setattr__

Aahz aahz at pythoncraft.com
Tue Jul 9 00:04:51 EDT 2002


In article <mailman.1025803028.29763.python-list at python.org>,
Tim Peters  <tim.one at comcast.net> wrote:
>
>The object dict (x's dict in the above, which has nothing to do with
>object's dict) is never searched for methods, and not in any version of
>Python.  Methods can only come from classes in Python.  If the name
>"somemethod" isn't found in x's class or any of its base classes, then
>"somemethod" is not a method of x.  "somemethod" may well be a data
>attribute of x that's bound to a function that "acts like" a method, or
>close enough to acting like a method that your app doesn't care about the
>difference, but even so it's not a method of x if you're careful with words.
>An object's (as opposed to the builtin class named object) dict in Python
>can only supply data attributes.

<scratch head>  Your argument appears to be failing my duck test:

    import new

    class C:
        def __init__(self, val):
            self.val = val

    def __str__(self):
        return str(self.val).upper()

    x = C('foo')
    x.__str__ = new.instancemethod(__str__, x, C)
    print str(x)

What am I missing?
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list