[Python-bugs-list] [ python-Bugs-623669 ] __rdiv__ vs new-style classes

SourceForge.net noreply@sourceforge.net
Fri, 03 Jan 2003 17:08:19 -0800


Bugs item #623669, was opened at 2002-10-15 14:00
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=623669&group_id=5470

Category: Type/class unification
Group: Python 2.2.2
Status: Open
Resolution: None
>Priority: 7
Submitted By: Tim Peters (tim_one)
Assigned to: Guido van Rossum (gvanrossum)
Summary: __rdiv__ vs new-style classes

Initial Comment:
In 2.2.2 and 2.3, consider this:

"""
class F:
    def __init__(self):
        print 'built an F'
    def __div__(self, other):
        print 'in F.__div__'
        return F()
    def __rdiv__(self, other):
        print 'in F.__rdiv__'
        return F() / self

class F2(F):
    def __init__(self):
        print 'built an F2'

3 / F2()
"""

This displays what I expect:

"""
built an F2
in F.__rdiv__
built an F
in F.__div__
built an F
"""

However, change F to derive from object:

class F(object):

and it's in infinite recursion, starting like so:

"""
built an F2
in F.__rdiv__
built an F
in F.__rdiv__
built an F
in F.__rdiv__
built an F
in F.__rdiv__
built an F
in F.__rdiv__
built an F
in F.__rdiv__
built an F
in F.__rdiv__
...
"""

Despite that F.__rdiv__ creates an explicit F() instance 
and uses it on the LHS of /, F.__div__ is never invoked; 
instead the RHS F2.__rdiv__ == F.__rdiv__ always gets 
the nod.

Maybe this is intentional?  I confess I've lost track of the 
rules.

Changing the end of F.__rdiv__ to

return F().__div__(self)

brute-forces the desired outcome, so there is a 
workaround.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=623669&group_id=5470