Skip to content

Instantly share code, notes, and snippets.

@theepicsnail
Created March 10, 2011 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theepicsnail/864466 to your computer and use it in GitHub Desktop.
Save theepicsnail/864466 to your computer and use it in GitHub Desktop.
Overriding += to make a call.
class Bar:
def __init__(self,inst,func,*args):
self.inst = inst
self.func = func
self.args = args
def __iadd__(self,*args,**kwargs):
return self.__call__(*args,**kwargs)
def __call__(self,*args,**kwargs):
self.func(self.inst,*(self.args+args),**kwargs)
class Foo:
def __init__(self):
self.debug = Bar(self,self.log,"DEBUG")
self.note = Bar(self,self.log,"NOTE")
def log(self,barself,level,msg):
print "Level:",level
print "Mesg :",msg
a= Foo()
a.debug("A")
a.note+="B"
output = """
Level: DEBUG
Mesg : A
Level: NOTE
Mesg : B
"""
raw_input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment