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

Patch for str __add__ not working #65

Open
dealbreaker973 opened this issue Apr 12, 2022 · 0 comments
Open

Patch for str __add__ not working #65

dealbreaker973 opened this issue Apr 12, 2022 · 0 comments

Comments

@dealbreaker973
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant