[Tutor] lambdas

Karl Pflästerer sigurd at 12move.de
Sun Dec 28 16:31:22 EST 2003


On 27 Dec 2003, Conrad Koziol <- arkamir at softhome.net wrote:

> How do you make a lambda expression that returns the time??

Would you mind telling us why you want something like that in Python?
Lambda expressions in Python are very weak (sadly).

> I got this 

>>>> lambda time: '%s-%s-%s: %s.%s:%s' % (time[0], time[1], time[2],
> time[3], time[4], time[5], time[6]), (time.localtime(time.time()))

> which returns this, which is not what I want.

> (<function <lambda> at 0x8a2791c>, (2003, 12, 27, 12, 30, 20, 5, 361,
> 0))

This is a tuple of two objects: your lambda form and the result of
(time.localtime(time.time()) (which is another tuple).

What you wanted to do (I think) was to call your lambda form with one
argument: the output from time.localtime(); you don't need to supply
time.time as argument since this is the default.

Furthermore your lambda form is buggy (you have six %s on the left side
but seven args on the right side.  Python won't like that.

> i need something like 2003-15-03 1.45:45

But why with a lambda form?

And you pick the wrong indices if you wanted such a form.
You want year, month, day but if you look at the docstring of
time.localtime you will find:

,----
| In [49]:  time.localtime? 
| Type:           builtin_function_or_method
| Base Class:     <type 'builtin_function_or_method'>
| String Form:    <built-in function localtime>
| Namespace:      Interactive
| Docstring:
|     localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
|     
|     Convert seconds since the Epoch to a time tuple expressing local time.
|     When 'seconds' is not passed in, convert the current time instead.
`----

So you need (indices given) 0,2,1,3,4,5

But here an example with lambda (although I don't think it's what you
really should do):

In [59]: (lambda t: '%s-%s-%s: %s.%s:%s' % (t[0], t[2], t[1], t[3], t[4], t[5]))(time.localtime())
Out[59]: '2003-28-12: 22.9:44'

Most of the time I like lambdas but here it seems to me unnecessary
ugly.  What happens is the creation of the lambda form:

(lambda t: '%s-%s-%s: %s.%s:%s' % (t[0], t[2], t[1], t[3], t[4], t[5]))

It's enclosed in parentheses so it can be called with an argument.

(time.localtime())

This is the argument to the lambda form and it is also written in
parentheses like nearly every function you call.

Furthermore ugly at the moment is that no minimum field width is given.
So seconds aren't written as eg. 09 but 9.  But that's easily corrected.


In [61]: (lambda t: '%s-%02i-%02i: %02i.%02i:%02i' % (t[0], t[2], t[1], t[3], t[4], t[5]))(time.localtime())


> How would I do this?? Also how do I get rid of this part:

see above.

> <function <lambda> at 0x8a2791c>

See above.  You didn't create a function and called it with an argument
but created a tuple of two objects: your function and the value of the
argument given.

> because i get an error message(too many arguements) when passing it to a
> function like this:

> def test(hello):
> 	print hello

How do you pass it?  Why do you write it so complicated?  I hope that's
no homework.



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list