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. 12 1583
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
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
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!
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
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
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.
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?
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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Oliver Fromme |
last post by:
Hi,
I'm trying to write a Python function that parses
an expression and builds a function tree from it
(recursively).
During parsing, lambda functions for the the terms
and sub-expressions...
|
by: Nick Coghlan |
last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0.
Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped
in favour of a different syntax,...
|
by: Kay Schluehr |
last post by:
Since George Sakkis proposed a new way of doing list comprehensions
http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/ac5023ad18b2835f/d3ff1b81fa70c8a7#d3ff1b81fa70c8a7
...
|
by: Matt |
last post by:
Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
advantage for one over the other.
delegate int F(int a);
F fLambda = a...
|
by: Pavel Minaev |
last post by:
On Aug 14, 1:37 pm, raylopez99 <raylope...@yahoo.comwrote:
You are, of course, entitled to your opinion, but lambdas are a part
of C# 3.0, and any C# developer has to know their ins and outs if...
|
by: Peter |
last post by:
Hi
I've been delving into "delegates" and "anonymous methods", and now
I've come across the term "closure".
Some information I've found says that C# does not have closures, other
information...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |