N
Nicodemus
Hi all,
I found a surprising behavior regarding new-style classes operator lookup.
It seems that for operators, the instance methods are ignored. Observe:
.... def foo(self):
.... print 'foo'
.... def __setitem__(self, k, v):
.... print 'C.__setitem__', k, v
....
........ print 'my_setitem', k, v
....
All is well. Now, if you use a new-style class, the instance method is not
called:
.... def foo(self):
.... print 'foo'
.... def __setitem__(self, k, v):
.... print 'C.__setitem__', k, v
....
........ print 'my_setitem', k, v
....
Is this a bug, or am I missing something? Any help would be appreciated.
Regards,
Nicodemus.
I found a surprising behavior regarding new-style classes operator lookup.
It seems that for operators, the instance methods are ignored. Observe:
.... def foo(self):
.... print 'foo'
.... def __setitem__(self, k, v):
.... print 'C.__setitem__', k, v
....
.... print 'my_foo'c = C()
c.foo() foo
c[1] = 1 C.__setitem__ 1 1
def my_foo():
........ print 'my_setitem', k, v
....
c.foo = my_foo
c.__setitem__ = my_setitem
c.foo() my_foo
c[1] = 1 my_setitem 1 1
All is well. Now, if you use a new-style class, the instance method is not
called:
.... def foo(self):
.... print 'foo'
.... def __setitem__(self, k, v):
.... print 'C.__setitem__', k, v
....
.... print 'my_foo'c = C()
c.foo() foo
c[1] = 1 C.__setitem__ 1 1
def my_foo():
........ print 'my_setitem', k, v
....
c.foo = my_foo
c.__setitem__ = my_setitem
c.foo() my_foo
c[1] = 1 C.__setitem__ 1 1 # should print "my_setitem 1 1"
Is this a bug, or am I missing something? Any help would be appreciated.
Regards,
Nicodemus.