JMK no matter what

Delayed parameter passing

함수에 다른 함수를 인자로 줘서 호출하게 할 때, 해당 인자의 디폴트 인자를 바꿔서 호출하게 하려면 lambda *x: myFunc(*x, keywordParam=newValue) 식으로 호출하면 된다. 근데 이러면 로그에는 weightGenerator=<function <lambda> at 0x6850938> 이런 식으로 나타나게 됨. 로그만으로 모든 것을 reproduce 할 수 있게 만든다는 목적에 어긋나게 된다. 짜증내 하다가 결국 오늘에야 짰다. ;;;

lang:py
class Call:
    def __init__(self, func, **kwargs):
        self.func = func 
        self.kwargs = kwargs
    def __call__(self, *args):
        return self.func(*args, **self.kwargs)
    def __repr__(self):
        fn = self.func.func_name if type(self.func) == types.FunctionType else str(self.func)
        return fn + "(" + ", ".join(["%s=%s" % (str(k),str(v)) for k, v in self.kwargs.iteritems()]) + ")"

Call(myFunction, kwParam1=7, kwParam2=8) 식으로 만들면 로그에도 똑같이 이쁘게 나온다. 'ㅅ'b 파이썬 캡이올시다 ㅠ.ㅠ 이거 C++ 로 하려면.. 아...

2010-01-12 02:35:30 | JM | /writings/cs/ | 0 Comments

Leave a comment