Skip to content
Advertisement

Python: inconsistence in the way you define the function __setattr__?

Consider this code:

JavaScript

I would expect the same output. However, with CPython 2.5, 2.6 (similarly in 3.2) I get:

JavaScript

With PyPy 1.5.0, I get the expected output:

JavaScript

Which is the “right” output? (Or what should be the output according to the Python documentation?)


Here is the bug report for CPython.

Advertisement

Answer

I suspect it has to do with a lookup optimization. From the source code:

JavaScript

The fast path does does not look it up on the class dictionary.

Therefore, the best way to get the desired functionality is to place an override method in the class.

JavaScript

Which I found works as expected.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement