Issue1783803
Created on 2007-08-29.07:12:11 by ayeshaiqbal, last changed 2007-09-19.23:06:16 by pjenvey.
msg2846 (view) |
Author: ayesha (ayeshaiqbal) |
Date: 2007-08-29.07:12:11 |
|
I have attached a patch which fixes this issue.Now the in and not in keywords will call check if the object is an instance of PyDictionary .If so has_key() will be called instead of __contains__()
|
msg2847 (view) |
Author: rajesh battala (rajesh_battala) |
Date: 2007-08-29.07:14:34 |
|
hi charles.
i have looked at the patch.
its working nice.!
thanks
|
msg2848 (view) |
Author: Philip Jenvey (pjenvey) |
Date: 2007-08-29.20:01:45 |
|
Your patch does indeed fix the original issue, but checking for an instance of PyDictionary in PyObject isn't the right solution here. PyObject doesn't need to know anything about its PyDictionary subclass when we can utilize polymorphism to make PyDictionary do the right thing
PyObject subclasses are made to be able to override PyObject methods when they want to implement their own custom versions (such as for performance reasons, like this issue) of Python methods
So PyDictionary needs to override __contains__ to call its own dict___contains__. PyObject's in/notin can stay the same; simply call __contains__
The following patch does this instead (I can't attach it here):
http://underboss.org/~pjenvey/patches/dict_contains_r3455.diff
It also changes dict___contains__ to call has_key instead of the final dict_has_key method -- so if a subclass overrides has_key, __contains__ will utilize the overridden version
|
msg2849 (view) |
Author: Samuele Pedroni (pedronis) |
Date: 2007-08-30.07:33:52 |
|
a note of warning, Python doesn't properly define what other methods behavior is influenced by overriding some other methods, indeed in CPython the usual answer in most cases is none:
Python 2.4.3 (#1, Apr 7 2006, 10:54:33)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class X(dict):
... def has_key(self, k):
... return (k % 2) == 0
...
>>>
>>> x=X()
>>> x.has_key(4)
True
>>> 4 in x
False
the risk in not following CPython in this and try to be more useful, is to easily create cases where in some situations you get what you wanted but in other overriding cases you get infinite recursions.
|
msg2850 (view) |
Author: ayesha (ayeshaiqbal) |
Date: 2007-08-31.04:59:57 |
|
Thanks for the valuable tip pjenvy.I have attached your patch .It does make a lot of sense to utilise polymorphic capabilties of java (though there are several places in jython where this rule is not followed).
I also ran Pedroni's code sample with both my patch and pjenvy's patch in jython as also python.I get the same output in all cases .
File Added: dict_contains_r3455.diff
|
msg2851 (view) |
Author: Philip Jenvey (pjenvey) |
Date: 2007-09-13.02:25:48 |
|
thanks for catching that pedronis
applied in r3473 minus the has_key change
|
msg2852 (view) |
Author: Pekka Klärck (pekka.klarck) |
Date: 2007-09-13.15:25:48 |
|
Could this patch be applied to 2.2 branch so that possible 2.2.1 will have the fix also?
Using "key in dictionary" format is considered more pythonic than "dictionary.has_key(key)" and on CPython it's also faster (see e.g. [1]). There's thus probably a lot of code using that format and it's a shame that it's so much slower in Jython 2.2.
[1] http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#use-in-where-possible-1
|
msg2853 (view) |
Author: Philip Jenvey (pjenvey) |
Date: 2007-09-19.23:06:16 |
|
Charlie has merged this to the 2.2 branch in r3484
|
|
Date |
User |
Action |
Args |
2007-08-29 07:12:11 | ayeshaiqbal | create | |
|