[Python-Dev] [Python-3000] Warning for 2.6 and greater

"Martin v. Löwis" martin at v.loewis.de
Wed Jan 17 00:08:56 CET 2007


A.M. Kuchling schrieb:
> What about turning all references to obj.items into the equivalent
> bytecode for this:
> 
> if isinstance(obj, dict): # XXX should this be 'type(obj) is dict'?
>    if <2.x behaviour>:   _temp = obj.items
>    elif <3.x behaviour>: _temp = obj.iteritems
> else:
>    _temp = obj.items
> 
> Ugly; very ugly.

This would "work", I think, although I'd rather add a tp_getattr3 slot
to types, as somebody else proposed: getattr3 would default to getattr
if not defined, and would look up iteritems when asked for iter on dict
objects.

It would still "suffer" from the cross-module issue:

# a.py
from __future__ import items_is_iterator
def f(d):
  return d.items
# b.py
import a
d = d{}
print a.f(d)()

For compatibility with 2.x, a.f should really return a bound
method that returns lists; for compatibility with 3.x, it will return
a method that produces an iterator.

Of course, one might say "don't do that, then".

Regards,
Martin


More information about the Python-Dev mailing list