https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Fi...
What I was speaking to is the desire to write:
class SomeExampleModel:
def setHeavyweightProperty:
triggers('cache:dirty')(
lambda self, property, value:
...something...
My understanding is that Python doesn't like two different things about this. First, the multi-line anonymous lambda being used as the target of the decorator. Second, a function being called with another function as its argument as an expression within an instance method definition.I'm open to reëducation.
setHeavyweightProperty = triggers('cache:dirty')(
lambda self, property, value: 'code')
is the same thing as @triggers('cache:dirty')
def setHeavyweightProperty(self, property, value):
# code
but with the limitations of lambdas (no statements allowed)> First, the multi-line anonymous lambda being used as the target of the decorator.
Python has no issue with that, it's just hard to do it because Python's lambdas can only contain expressions which can be rather limited in a statements-heavy language.
> Second, a function being called with another function as its argument as an expression within an instance method definition.
That's because your "instance method definition" is not syntactically correct, the capability itself exists. A "method definition" is nothing more than a function defined within a class scope. In fact you can do things like that:
>>> def method_outside(self):
... return self.wedding
...
>>> class SomeType:
... whose = method_outside
...
>>> s = SomeType()
>>> s.wedding = "Amanda"
>>> s.whose()
'Amanda'
>>> method_outside(s)
'Amanda'
>>> from collections import namedtuple
>>> o = namedtuple('N', 'wedding')(wedding="Jim")
>>> method_outside(o)
'Jim'
>>>The only thing python does not like is multi-line lambdas. You can target a lambda with decorators, just like you can target any callable. I'm not sure if I correctly parsed what you mean by "a function being called with another function as its argument as an expression within an instance method definition", but I'm pretty sure python can do that. Here is some demonstration code (see RaganwaldWidgetViewThree):
https://gist.github.com/3495990
And finally here is the translation of the before/after/etc. method combinators:
https://gist.github.com/3495985
But I might have misunderstood waitLevel, can you explain what it is for?
ifAuthorized("admin", lambda x:
// more code hereTechnically, Python's lambdas are one-expression not one-line.
Within that constraint you can create as complex lambdas as you want (you'll probably need to create helpers for things like loops, and you may be hindered by Python's limits on recursion, but FWIW Scheme's or Haskell's functions have the exact same limitation, the primary difference being they're nowhere near as statement-heavy as Python).
There's no point in having an anonymous function decorator. You'd simply inline it into the body itself right on the spot, anything else would simply be obfuscation for the sake of obfuscation. This only makes sense if you're using a decorator applied elsewhere, at which point all languages require you to have named it, so there's no point complaining about lack of anonymous functions here.
Python has first-class functions. It even has first-class methods, with automatic instance binding. It just doesn't quite work how very functionally-oriented people want it to work. Part of the reason I don't like the kvetching that such people do is that it does seem to convince people that Python is lacking function references. Nope. It just doesn't spell them to everybody's taste (for instance, "it doesn't have Ruby blocks" translates to "it doesn't have block syntax", not "it can't do function references", and this is a taste issue not a fundamental capability issue), and the can rarely have moderately inconvenient scope rules if you want to write to outer scopes.
Decorators are syntax sugar, not a new feature.