Essentially this is what I want to accomplish:
class Move(object):
 def __init__(self, Attr):
 if Attr:
 self.attr = Attr
 if hasattr(self, "attr"):
 __call__ = self.hasTheAttr
 else:
 __call__ = self.hasNoAttr
 def hasNoAttr(self):
 #no args!
 def hasTheAttr(func, arg1, arg2):
 #do things with the args
 __call__ = hasNoAttr
I know that that doesn't work, it just uses hasNoAttr all the time. My first thought was to use a decorator, but I'm not all that familiar with them and I couldn't figure out how to base it from whether or not a class attribute existed or not.
Actual question part: How would I be able to deterministically make a function either x function or y function depending on a condition.
- 
 Could I ask what the use case is then?Jon Clements– Jon Clements2013年01月29日 19:19:36 +00:00Commented Jan 29, 2013 at 19:19
 - 
 this is for movement code, where the parent object may or may not have a collision attribute. In _call_, when you have the attribute, two arguments are needed, none are needed if its there's no attribute. I wanted to do it this way rather than having default arguments so that it would error if I forgot to give those arguments to the one's that needed it (whereas you wouldn't get such an error if I was using default argumentsDanielCardin– DanielCardin2013年01月29日 19:46:10 +00:00Commented Jan 29, 2013 at 19:46
 
1 Answer 1
You can't really do this sort of thing with __call__ -- with other (non-magic) methods, you can just monkey-patch them, but with __call__ and other magic methods you need to delegate to the appropriate method within the magic method itself:
class Move(object):
 def __init__(self, Attr):
 if Attr:
 self.attr = Attr
 if hasattr(self, "attr"):
 self._func = self.hasTheAttr
 else:
 self._func = self.hasNoAttr
 def hasNoAttr(self):
 #no args!
 def hasTheAttr(func, arg1, arg2):
 #do things with the args
 def __call__(self,*args):
 return self._func(*args)