Created
September 30, 2018 15:35
-
-
Save hzhangxyz/b6ed53a3dfe49875d9bac88995ae6c9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class metaA(type): | |
def __new__(cls, n, b, a): | |
#a["__add__"] = lambda x,y:4 | |
obj = type.__new__(cls, n, b, a) | |
#obj.__add__ = lambda x,y:4 | |
return obj | |
def __getattr__(self, name): | |
if name == "__add__": | |
return lambda x,y:4 | |
raise AttributeError() | |
#def __add__(self, a): | |
# return 4 | |
class A(object, metaclass=metaA): | |
def __init__(self): | |
setattr(self,'__add__',(lambda x,y:5).__get__(self,A)) | |
a = A() | |
print(a.__add__(a)) | |
# 5 | |
print(type(a).__add__(a,a)) | |
# 4 | |
print(a + a) | |
# TypeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment