Ant <[EMAIL PROTECTED]> wrote: > > Thanks, that's awesome! Definitely not something I'd have ever been able > > to work out myself - I think I need to learn more about nested functions > > and introspection. > > I've recently found nested functions incredibly useful in many places > in my code, particularly as a way of producing functions that are > pre-set with some initialization data. I've written a bit about it > here: http://antroy.blogspot.com/ (the entry about Partial Functions) > > > > def memoizeMethod(cls, n, m): > > > def decorated(self): > > > if n in self._memo: return self._memo[n] > > > result = self._memo[n] = m(self) > > > return result > > > decorated.__name__ = n > > > setattr(cls, n, decorated) > > Couldn't this be more simply written as: > > def memoizeMethod(cls, n, m): > def decorated(self): > if not n in self._memo: > self._memo[n] = m(self) > return self._memo[n] > decorated.__name__ = n > setattr(cls, n, decorated)
Yes, at a tiny performance cost (probably hard to measure), though I would spell the guard "if n not in self._memo:" :-). > I've not seen the use of chained = statements before. Presumably it > sets all variables to the value of the last one? It sets all ``variables'' ("left-hand sides", LHS) to the right-hand side (RHS) value; here, it lets you avoid an extra indexing in order to obtain the value to return (a tiny cost, to be sure). Alex -- http://mail.python.org/mailman/listinfo/python-list