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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to