Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Bug overriding operators in new-style classes?

Reply
Thread Tools

Bug overriding operators in new-style classes?

 
 
Nicodemus
Guest
Posts: n/a
 
      07-17-2003
Hi all,

I found a surprising behavior regarding new-style classes operator lookup.
It seems that for operators, the instance methods are ignored. Observe:

>>> class C:

.... 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'
....
>>> def my_setitem(k, v):

.... 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:

>>> class C(object):

.... 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'
....
>>> def my_setitem(k, v):

.... 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional overriding of operators Daniel Vartanov Ruby 7 11-04-2008 04:27 PM
Re: Overriding logical operators? Terry Reedy Python 2 08-24-2004 05:49 AM
Fwd: Overriding logical operators? Andrew Durdin Python 1 08-24-2004 05:08 AM
Re: Overriding logical operators? Andrew Durdin Python 1 08-23-2004 04:24 PM
Overriding logical operators? Andrew Durdin Python 3 08-23-2004 02:56 PM



Advertisments