Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New style vs. old style classes __ror__() operator overloading #46361

Closed
calvin mannequin opened this issue Feb 13, 2008 · 3 comments
Closed

New style vs. old style classes __ror__() operator overloading #46361

calvin mannequin opened this issue Feb 13, 2008 · 3 comments
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@calvin
Copy link
Mannequin

calvin mannequin commented Feb 13, 2008

BPO 2102
Nosy @freddrake, @amauryfa
Files
  • t.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-06-17.02:08:01.094>
    created_at = <Date 2008-02-13.14:51:15.002>
    labels = ['type-bug', 'docs']
    title = 'New style vs. old style classes __ror__()  operator overloading'
    updated_at = <Date 2010-06-17.02:08:01.093>
    user = 'https://bugs.python.org/calvin'

    bugs.python.org fields:

    activity = <Date 2010-06-17.02:08:01.093>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-06-17.02:08:01.094>
    closer = 'terry.reedy'
    components = ['Documentation']
    creation = <Date 2008-02-13.14:51:15.002>
    creator = 'calvin'
    dependencies = []
    files = ['9425']
    hgrepos = []
    issue_num = 2102
    keywords = []
    message_count = 3.0
    messages = ['62361', '62362', '62379']
    nosy_count = 3.0
    nosy_names = ['fdrake', 'calvin', 'amaury.forgeotdarc']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue2102'
    versions = ['Python 2.5', 'Python 2.4']

    @calvin
    Copy link
    Mannequin Author

    calvin mannequin commented Feb 13, 2008

    Hi,

    the attached code in t.py fails to run:
    class C (object):
    def __ror__ (self, other):
    return 42
    print C() | C()

    $ python t.py
    Traceback (most recent call last):
      File "t.py", line 5, in ?
        print C() | C()
    TypeError: unsupported operand type(s) for |: 'C' and 'C'

    If I use old style classes (ie. "class C:" instead of "class
    C(object):"), the code runs fine. I suspect that the method lookup for
    special operator methods is different in new style classes, but why?
    This might also be related to issue bpo-643841 but I am not sure.

    @calvin calvin mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 13, 2008
    @amauryfa
    Copy link
    Member

    Doc says:
    http://docs.python.org/dev/reference/datamodel.html#object.\_\_ror__

    """
    These functions are only called if the left operand does not support the
    corresponding operation and the operands are of different types.

    For operands of the same type, it is assumed that if the non-reflected
    method (such as __add__()) fails the operation is not supported, which
    is why the reflected method is not called.
    """

    So I'd say the opposite: this is an old-style class problem.

    @calvin
    Copy link
    Mannequin Author

    calvin mannequin commented Feb 14, 2008

    Ah yes, I did not realize the "different types" part. So it is indeed an
    old-style class problem, which should behave just like the new-style
    classes but they don't.

    However I would probably not fix this in the 2.x series of Python.
    Changing the behaviour would break compatibility. The documentation
    should mention the different behaviour though.

    @calvin calvin mannequin changed the title New style classes __ror__() operator overloading problem New style vs. old style classes __ror__() operator overloading Feb 14, 2008
    @jafo jafo mannequin added docs Documentation in the Doc dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 19, 2008
    @jafo jafo mannequin assigned freddrake Mar 19, 2008
    @freddrake freddrake removed their assignment Dec 4, 2008
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants