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 6 1471
George Yoshida wrote: 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: def x(): pass
.... y = x y.__name__
'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?
--
Michael Hoffman
Michael Hoffman wrote: Anyway I don't think it has anything to do with lambda:
>>> def x(): pass ... >>> y = x >>> y.__name__ '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.
But why?
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
"George Yoshida" <ml@dynkin.com> wrote in message
news:ck***********@dojima-n0.hi-ho.ne.jp... 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
George Yoshida wrote: 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)
--
Michael Hoffman
>>>>> George Yoshida <ml@dynkin.com> (GY) wrote:
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.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl
George Yoshida <ml@dynkin.com> wrote: 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"?
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stephen Thorne |
last post by:
Hi guys,
I'm a little worried about the expected disappearance of lambda in
python3000. I've had my brain badly broken by functional programming
in the past, and I would hate to see things...
|
by: André |
last post by:
Short version of what I am looking for:
Given a class "public_class" which is instantiated a few times e.g.
a = public_class()
b = public_class()
c = public_class()
I would like to find...
|
by: Paddy McCarthy |
last post by:
Hi,
I am trying to use eval as little as possible but solve this problem.
#If given:two or more lambda equations
x=lambda : A < B
y=lambda : C+6 >= 7
....
How do I create another lambda...
|
by: Tom Anderson |
last post by:
Comrades,
During our current discussion of the fate of functional constructs in
python, someone brought up Guido's bull on the matter:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
...
|
by: Max Rybinsky |
last post by:
Hello!
Please take a look at the example.
>>> a = # Just a list of tuples
>>> a
Now i want to get a list of functions x*y/n, for each (x, y) in a:
|
by: jena |
last post by:
hello,
when i create list of lambdas:
l=]
then l() returns 'C', i think, it should be 'A'
my workaround is to define helper class with __call__ method:
class X:
def __init__(self,s): self.s=s...
|
by: Xah Lee |
last post by:
A Lambda Logo Tour
(and why LISP languages using λ as logo should not be looked upon
kindly)
Xah Lee, 2002-02
Dear lispers,
The lambda character λ, always struck a awe in me, as with...
|
by: Kaz Kylheku |
last post by:
I've been reading the recent cross-posted flamewar, and read Guido's
article where he posits that embedding multi-line lambdas in
expressions is an unsolvable puzzle.
So for the last 15 minutes...
|
by: Tim H |
last post by:
Compiling with g++ 4:
This line:
if_then_else_return(_1 == 0, 64, _1)
When called with a bignum class as an argument yields:
/usr/include/boost/lambda/if.hpp: In member function 'RET...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |