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. 0 1146 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andrew Durdin |
last post by:
In Python, you can override the behaviour of most operators for a
class, by defining __add__, __gt__, and the other special object
methods.
I noticed that, although there are special methods for...
|
by: Cheng Mo |
last post by:
When overriding operator new & delte of one class, the method is
implicitly declared as static. However, overriding operator new & delete
of template cannot be static.The compiler says cannot...
|
by: shoosh |
last post by:
hi
for my application under VC6 I need to implement my own memory manager
which overrides the global new and delete operators and which Do Not
use the normal free() and malloc().
it seemed to...
|
by: Gonçalo Rodrigues |
last post by:
Hi all,
I have a base class, call it Object, that implements operators new and
delete.
Now suppose there is also a class, call it Derived, deriving from
Object. It has the feature that *all*...
|
by: groleo |
last post by:
Hi list.
Simple question:
is it possible to override the global new/delete operators, without
using malloc/free?
I mean something in the ideea of the code below, which doesnt work
cause of...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |