get the name of lambda

G

George Yoshida

Just out of curiosity, is there any way to get the name of a
lambda expression? Lambdas are anonymous functions, so it's a
stupid idea to get the name of it. But if it's possible, how
could I?

Let's take a simple example:
foo = lambda:None

foo.__name__(or foo.func_name) returns '<lambda>'.
I'm looking for a way to return 'foo', instead of '<lambda>'.

Thanks in advance.

-- George
 
M

Michael Hoffman

George said:
Just out of curiosity, is there any way to get the name of a
lambda expression? Lambdas are anonymous functions, so it's a
stupid idea to get the name of it. But if it's possible, how
could I?

Let's take a simple example:
foo = lambda:None

foo.__name__(or foo.func_name) returns '<lambda>'.
I'm looking for a way to return 'foo', instead of '<lambda>'.

But you already know that it is called 'foo'! :)

Anyway I don't think it has anything to do with lambda:
'x'

IIRC, in 2.4 you can set the func_name attribute so you could set up a
decorator to set the name for you. Or if you want to be fancy you could
even set up a metaclass that automatically renames all the lambda
functions it finds in a class.

But why?
 
G

George Yoshida

Michael said:
Anyway I don't think it has anything to do with lambda:

'x'

IIRC, in 2.4 you can set the func_name attribute so you could set up a
decorator to set the name for you. Or if you want to be fancy you could
even set up a metaclass that automatically renames all the lambda
functions it finds in a class.

I haven't had a chance to play with decorators, but I'll give
it a try. In any case, I don't need a fancy way.
The simpler, the better.

I implemented a function in several ways(using recursion,
lambda, etc) and benchmarked them. In that script, I needed
to display function names, along with ellapsed time.

I was doing something like:

for func in (func1, func2, func3):
print func.func_name
print measure_ellapsed_time(func)

and get hit by lambda!

-- George
 
T

Terry Reedy

George Yoshida said:
Just out of curiosity, is there any way to get the name of a
lambda expression? Lambdas are anonymous functions, so it's a
stupid idea to get the name of it. But if it's possible, how
could I?

Let's take a simple example:
foo = lambda:None

foo.__name__(or foo.func_name) returns '<lambda>'.
I'm looking for a way to return 'foo', instead of '<lambda>'.

Use 'def foo: return None' instead. This is precisely why a 'name = lambda
....' statement is inferior to a def statement.

[and in followup]
I implemented a function in several ways(using recursion,
lambda, etc) and benchmarked them. In that script, I needed
to display function names, along with ellapsed time.

So use a def instead of lambda. Only use lambda when you have an
expression to return and *do not need* a name to display. Python is not
Lisp ;-)

Terry J. Reedy
 
M

Michael Hoffman

George said:
I implemented a function in several ways(using recursion,
lambda, etc) and benchmarked them. In that script, I needed
to display function names, along with ellapsed time.

To be honest, I think munging the func_name attribute is a silly way to
get what you want :)
I was doing something like:

for func in (func1, func2, func3):
print func.func_name
print measure_ellapsed_time(func)

How about:

funcs = dict(func1=func1, func2=func2, func3=lambda: None)
for name, func in funcs.iteritems():
print name
print measure_elapsed_time(func)
 
P

Piet van Oostrum

GY> Just out of curiosity, is there any way to get the name of a
GY> lambda expression? Lambdas are anonymous functions, so it's a
GY> stupid idea to get the name of it. But if it's possible, how
GY> could I?

You already said it: it's a stupid idea. Anonymous means: having no name,
so what is the name of an anonymous thing supposed to be.

GY> Let's take a simple example:
GY> foo = lambda:None

In your example foo is not the name of the lambda. It is just a variable
whose value is the lambda expression.
 
T

Tim Roberts

George Yoshida said:
Just out of curiosity, is there any way to get the name of a
lambda expression? Lambdas are anonymous functions, so it's a
stupid idea to get the name of it. But if it's possible, how
could I?

Let's take a simple example:
foo = lambda:None

foo.__name__(or foo.func_name) returns '<lambda>'.
I'm looking for a way to return 'foo', instead of '<lambda>'.

Can't be done. "foo" is just a name of a reference to something. The
lambda function has no idea that there happens to be a name called "foo"
that is bound to it. Consider this:

foo = boo = doo = lambda:None

def printMyName(zippy):
print zippy.__name__

printMyName( foo )
printMyName( boo )
printMyName( doo )

printMyName will get called 3 times with the exact same object. Inside
printMyName, would you expect the first call to print "foo"? Why not
"zippy"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top