Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Bug overriding operators in new-style classes? (http://www.velocityreviews.com/forums/t319833-bug-overriding-operators-in-new-style-classes.html)

Nicodemus 07-17-2003 07:05 PM

Bug overriding operators in new-style classes?
 
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.




All times are GMT. The time now is 04:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.