Skip to content

Patch for str __add__ not working #65

Description

@dealbreaker973

I want to modify __add__ magic function for str, but somehow it is not working. Refer to a previous issue #21, the code below is working fine:

from forbiddenfruit import curse

def __add__(self, a):
    """
        this is the test
    """
    if isinstance(a, bytes):
        self += a.decode('utf-8')
    else:
        self += a


curse(str, '__add__', __add__)

s = "sample string"a
print(s + "encode string".encode('utf-8'))

However, I discovered that if you try to do "a" + "b" instead of "a" + b"b", the modified function is not called (I tried to print something out inside the new __add__ function). It turns out the modified function only works when the type of the second variable is not str.

Any idea why this happens? By the way, I tried on Python 3.8.10

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions