From: Trans Date: 2007-08-29T00:10:56+09:00 Subject: Re: python-style decorators On Aug 7, 1:45 pm, "Keith Rarick" wrote: > Perhaps the following notation would be better: > > class C > decorate :memoized > def tak(x, y, z) > return z if x <= y > tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)) > end > end > > It would obviate the need to redefine thedecoratormethod itself and > thus simplify the implementation. Also, this notation is more explicit > about the mechanism. That's not a bad idea really. It would make it clear when a decorator is being used too. > > Though it's a bit less convenient, it might be better to > > just name the method: > > > class C > > def tak(x, y, z) > > return z if x <= y > > tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)) > > end > > memoized :tak > > end > > > Unfortunately, not as nice, but the underlying code would > > certainly get simplified. > > Yes, the implementation would be pretty easy. (There's even a similar > example, called "once", in the pickaxe book.) However, putting thedecoratorat the bottom makes it easy to miss, especially if the > method body is long. > > > Dreaming a little. I wonder, if there were a callback for when > > a class/module closes, then maybe you do do it lazily? > > Yeah, when I started thinking about how to do this I looked for such a > callback but didn't find one. > > > Also, I > > wonder if this corresponds to Matz' idea of ":"-notation he > > used for pre and post. So, > > > class C > > def tak:memoized(x, y, z) > > return z if x <= y > > tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)) > > end > > end > > That's very interesting! I didn't know about that notation before. I > just read about pre, post, and wrap methods, which seem similar but > less useful. They are invoked at the method call, rather than the > method definition, so they have less chance to affect the method's > interface. > > Now, if you could define arbitrary methods to be used with the > ":"-notation, like def tak:memoized(x, y, z) in your example above, > that would be really useful. I think it could be pretty useful too. Haven't heard Matz talk about this notation in along time though. > > Oh, one last thing. Could you give some other examples? [snip] Excellent examples, thank you. If your okay with it, I'd like to give this consideration for inclusion in Facets. T.