Bug overriding operators in new-style classes?

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
....
c = C()
c.foo() foo
c[1] = 1 C.__setitem__ 1 1
def my_foo():
.... print '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
....
c = C()
c.foo() foo
c[1] = 1 C.__setitem__ 1 1
def my_foo():
.... print '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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top