Skip to content

Instantly share code, notes, and snippets.

@hzhangxyz
Created September 30, 2018 15:35
Show Gist options
  • Save hzhangxyz/b6ed53a3dfe49875d9bac88995ae6c9b to your computer and use it in GitHub Desktop.
Save hzhangxyz/b6ed53a3dfe49875d9bac88995ae6c9b to your computer and use it in GitHub Desktop.
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