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.
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: