The example I gave earlier is a bit contrived, the real example
fundamentally requires a lambda since I am actually passing in local
variables into the functions the lambda is wrapping. Example:

def MyFunction():
  localVariable = 20
  CreateTask( lambda: SomeOtherFunction( localVariable ) ) # CreateTask
() executes the functor internally

This is more or less like the real scenario I'm working with.

You could define a local function ('closure') inside the function:

def MyFunction():
        localVariable = 20
        def UseLocalVariable():
                SomeOtherFunction(localVariable)
        CreateTask(UseLocalVariable)

I would suggest this is much better for readability.

On Tue, 18 Aug 2009 13:51:19 -0700, Robert Dailey <rcdai...@gmail.com> wrote:

On Aug 18, 3:45 pm, "Rami Chowdhury" <rami.chowdh...@gmail.com> wrote:
Lambda expressions are, I believe, syntactically limited to a single  
expression -- no statements, like 'print' is in Python 2.x.

If you are strongly against just defining a function, you might have to  
use a trick to get around it -- this page  
(http://p-nand-q.com/python/stupid_lambda_tricks.html) has some  
suggestions.

On Tue, 18 Aug 2009 13:32:55 -0700, Robert Dailey <rcdai...@gmail.com>  
wrote:





> On Aug 18, 3:31 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
>> Robert Dailey <rcdai...@gmail.com> wrote:
>> > Hello,

>> > I want to simply wrap a function up into an object so it can be called >> > with no parameters. The parameters that it would otherwise have taken
>> > are already filled in. Like so:

>> >       print1 = lambda: print( "Foobar" )
>> >       print1()

>> > However, the above code fails with:

>> >   File "C:\IT\work\distro_test\distribute_radix.py", line 286
>> >     print1 = lambda: print( "Foobar" )
>> >                          ^
>> > SyntaxError: invalid syntax

>> > How can I get this working?

>> def print1():
>>     print "Foobar"

>> It looks like in your version of Python "print" isn't a function. It  
>> always
>> helps if you say the exact version you are using in your question as the
>> exact answer you need may vary.

> I'm using Python 2.6. And using the legacy syntax in the lambda does
> not work either. I want to avoid using a def if possible. Thanks.

--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

The example I gave earlier is a bit contrived, the real example
fundamentally requires a lambda since I am actually passing in local
variables into the functions the lambda is wrapping. Example:

def MyFunction():
  localVariable = 20
  CreateTask( lambda: SomeOtherFunction( localVariable ) ) # CreateTask
() executes the functor internally

This is more or less like the real scenario I'm working with. There
are other (more personal) reasons why I prefer to avoid 'def' in this
case. I want to keep the functor as central to the code that needs it
as possible to improve code readability.

Thanks for the help everyone. I guess in Python 3.0 the print()
function will not require the import from __future__ to work in this
particular case?



--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to