Created
March 10, 2011 17:05
-
-
Save theepicsnail/864466 to your computer and use it in GitHub Desktop.
Overriding += to make a call.
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 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