469,610 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

anonymous functions/expressions without lambda?

I see lambda is "going away", so I want to use something that will be
around for awhile.

All I want to do is provide an "inline function" as an argument to
another function.

For example, let's say I have a function which binds a key to a function
call. I want to do something "simple" in this function call, and I have
a lot of bindings, so I don't want to have a ton of tiny little
functions scattered around:

def setVarTo1():
foo.var = 1
def setVarTo2():
foo.var = 2

bind('a', setVarTo1)
bind('b', setVarTo2)

Instead, I'd like to do something like this:

bind('a', foo.var = 1)
bind('b', foo.var = 2)

What's the recommended way to do something like this?

Note that the bind function is implemented in "C", and I am embedding
the interpreter.

Jul 19 '05 #1
12 1467
Paul Miller wrote:
For example, let's say I have a function which binds a key to a function
call. I want to do something "simple" in this function call, and I have
a lot of bindings, so I don't want to have a ton of tiny little
functions scattered around:

def setVarTo1():
foo.var = 1
def setVarTo2():
foo.var = 2

bind('a', setVarTo1)
bind('b', setVarTo2)

Instead, I'd like to do something like this:

bind('a', foo.var = 1)
bind('b', foo.var = 2)

What's the recommended way to do something like this?


This meets your requirements as stated:

def temp():
foo.var = 1

bind('a', temp)

def temp():
foo.var = 2

bind('b', temp)

del temp
-Peter
Jul 19 '05 #2
Paul Miller wrote:
I see lambda is "going away", so I want to use something that will be
around for awhile.

All I want to do is provide an "inline function" as an argument to
another function.
That's what lambda does. But it's going away, you'll have to use def
when it does, unless the language designers come up with something better.
For example, let's say I have a function which binds a key to a function
call. I want to do something "simple" in this function call, and I have
a lot of bindings, so I don't want to have a ton of tiny little
functions scattered around:

def setVarTo1():
foo.var = 1
def setVarTo2():
foo.var = 2

bind('a', setVarTo1)
bind('b', setVarTo2)
If a lot of the bindings are actually setting variables, you could do
something like this:

def attrsetter(obj, name, 1):
def _return_func(value):
return setattr(obj, name, value)

return _return_func
Instead, I'd like to do something like this:

bind('a', foo.var = 1)
bind('b', foo.var = 2)


bind('a', attrsetter(foo, "var", 1))
bind('a', attrsetter(foo, "var", 2))
--
Michael Hoffman
Jul 19 '05 #3
Michael Hoffman wrote:
That's what lambda does. But it's going away, you'll have to use def
when it does, unless the language designers come up with something better.
Yeah, I'm using lamda now it works nicely/cleanly.
If a lot of the bindings are actually setting variables, you could do
something like this:

def attrsetter(obj, name, 1):
def _return_func(value):
return setattr(obj, name, value)

return _return_func

bind('a', attrsetter(foo, "var", 1))
bind('a', attrsetter(foo, "var", 2))


Ah, perfect. Thanks!

Jul 19 '05 #4
Michael Hoffman wrote:
Paul Miller wrote:
> I see lambda is "going away", so I want to use something that will be
> around for awhile.
>
> All I want to do is provide an "inline function" as an argument to
> another function.


That's what lambda does. But it's going away, you'll have to use def
when it does, unless the language designers come up with something better.
For example, let's say I have a function which binds a key to a
function call. I want to do something "simple" in this function call,
and I have a lot of bindings, so I don't want to have a ton of tiny
little functions scattered around:

def setVarTo1():
foo.var = 1
def setVarTo2():
foo.var = 2

bind('a', setVarTo1)
bind('b', setVarTo2)

If a lot of the bindings are actually setting variables, you could do
something like this:

def attrsetter(obj, name, 1):
def _return_func(value):
return setattr(obj, name, value)

return _return_func


I think you meant to write something like this:

def attrsetter(obj, name, value):
def _return_func():
return setattr(obj, name, value)
return _return_func

Or, if you want to delay binding of the "value" parameter:

def attrsetter(obj, name):
def _return_func(value):
return setattr(obj, name, value)
return _return_func

Cheers,
Dave
Jul 19 '05 #5
Dave Benjamin wrote:
I think you meant to write something like this:

def attrsetter(obj, name, value):
def _return_func():
return setattr(obj, name, value)
return _return_func


Sure did. Sorry.
--
Michael Hoffman
Jul 19 '05 #6
In article <ae********************@powergate.ca>,
Peter Hansen <pe***@engcorp.com> wrote:
Paul Miller wrote:
For example, let's say I have a function which binds a key to a function
call. I want to do something "simple" in this function call, and I have
a lot of bindings, so I don't want to have a ton of tiny little
functions scattered around:

def setVarTo1():
foo.var = 1
def setVarTo2():
foo.var = 2

bind('a', setVarTo1)
bind('b', setVarTo2)

Instead, I'd like to do something like this:

bind('a', foo.var = 1)
bind('b', foo.var = 2)

What's the recommended way to do something like this?


This meets your requirements as stated:

def temp():
foo.var = 1

bind('a', temp)

def temp():
foo.var = 2

bind('b', temp)

del temp
-Peter


Ewww! *When* is lambda going bye-bye? I apparently
haven't been paying close enough attention. Among other
considerations, I still instruct people to use lambda for
plenty of specific cases.
Jul 19 '05 #7
Michael Hoffman wrote:
Dave Benjamin wrote:
I think you meant to write something like this:

def attrsetter(obj, name, value):
def _return_func():
return setattr(obj, name, value)
return _return_func

Sure did. Sorry.


You guys have been very helpful!

While on the subject, is there an equivalent for "methodcaller"?

ie. if I want to bind a function which calls a specific method of an
object with a specific parameter?

Jul 19 '05 #8
Paul Miller wrote:
Michael Hoffman wrote:
Dave Benjamin wrote:
I think you meant to write something like this:

def attrsetter(obj, name, value):
def _return_func():
return setattr(obj, name, value)
return _return_func


Sure did. Sorry.

You guys have been very helpful!

While on the subject, is there an equivalent for "methodcaller"?

ie. if I want to bind a function which calls a specific method of an
object with a specific parameter?


You could use a combination of bound methods and the "curry" function
defined in the Python Cookbook:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52549

The examples in the discussion do just that.

Dave
Jul 19 '05 #9
Paul Miller wrote:
While on the subject, is there an equivalent for "methodcaller"?

ie. if I want to bind a function which calls a specific method of an
object with a specific parameter?


def funccaller(func, *args, **kwargs):
def _return_func():
return func(*args, **kwargs)
return _return_func

class Test1(object):
def __init__(self, x):
self.x = x
def method(self, a, b, c):
return self.x + a + b + c

t1 = Test1(42)

funccaller_result = funccaller(t1.method, 3, 4, c=5)
funccaller_result()

And this time I actually tested it, and it works! ;)
--
Michael Hoffman
Jul 19 '05 #10
Dave Benjamin wrote:
You could use a combination of bound methods and the "curry" function
defined in the Python Cookbook:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52549

The examples in the discussion do just that.


Also, in the CVS version of Python, there's a new module called
"functional" with a function called "partial" that does the same thing
as far as I can tell. So, in the future, this function will probably be
available in the standard library.

Dave
Jul 19 '05 #11
Cameron Laird wrote:
In article <ae********************@powergate.ca>,
Peter Hansen <pe***@engcorp.com> wrote:
This meets your requirements as stated:

def temp():
foo.var = 1

bind('a', temp)

def temp():
foo.var = 2

bind('b', temp)

del temp


Ewww! *When* is lambda going bye-bye? I apparently
haven't been paying close enough attention. Among other
considerations, I still instruct people to use lambda for
plenty of specific cases.


Well, IMNSHO, it's an empty threat that's been looming on the horizon
for several years now. ;) Most recently, Guido mentioned his desire to
remove the keyword in his post on Artima, which resulted in a huge debate:

http://www.artima.com/weblogs/viewpost.jsp?thread=98196

Note that in the OP's question, as with Peter's example above, you still
can't do it with lambda alone, since you can't mix expressions with
assignment. Nonetheless, lambda is nice for delaying binding, and
combined with a helper function that sets attributes, it can produce the
most concise solution:

bind('a', lambda: setattr(foo, 'var', 1))
bind('b', lambda: setattr(foo, 'var', 2))

The usual response is that you probably want to make a class anyway, and
use bound methods for this type of stuff. But I find that in GUI/async
programming, often you need a little dab of glue to connect events
between objects, and lambda provides a nice way to avoid
over-abstracting the problem.

We'll see how this all pans out in the next few years. I'm not too fond
of removing features from a language for purely aesthetic reasons, but
lambda's really stuck in a syntactic quandry due to the
statement/expression dichotomy, so I can understand to some extent
Guido's desire to remove it.

Dave
Jul 19 '05 #12
Michael Hoffman wrote:
Paul Miller wrote:
While on the subject, is there an equivalent for "methodcaller"?

ie. if I want to bind a function which calls a specific method of an
object with a specific parameter?

def funccaller(func, *args, **kwargs):
def _return_func():
return func(*args, **kwargs)
return _return_func


....
And this time I actually tested it, and it works! ;)


Wow! Amazing. Yer right, it works!

Man, I LOVE this language.

Jul 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

53 posts views Thread by Oliver Fromme | last post: by
76 posts views Thread by Nick Coghlan | last post: by
37 posts views Thread by Kay Schluehr | last post: by
reply views Thread by Pavel Minaev | last post: by
4 posts views Thread by Peter | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.