get the name of lambda

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Yoshida

    #1

    get the name of lambda

    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
  • Michael Hoffman

    #2
    Re: get the name of lambda

    George Yoshida wrote:
    [color=blue]
    > 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>'.[/color]

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

    Anyway I don't think it has anything to do with lambda:
    [color=blue][color=green][color=darkred]
    >>> def x(): pass[/color][/color][/color]
    ....[color=blue][color=green][color=darkred]
    >>> y = x
    >>> y.__name__[/color][/color][/color]
    '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

    Comment

    • George Yoshida

      #3
      Re: get the name of lambda


      Michael Hoffman wrote:
      [color=blue]
      > Anyway I don't think it has anything to do with lambda:
      >[color=green][color=darkred]
      > >>> def x(): pass[/color][/color]
      > ...[color=green][color=darkred]
      > >>> y = x
      > >>> y.__name__[/color][/color]
      > '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.[/color]

      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.
      [color=blue]
      > But why?[/color]

      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_ellapse d_time(func)

      and get hit by lambda!

      -- George

      Comment

      • Terry Reedy

        #4
        Re: get the name of lambda


        "George Yoshida" <ml@dynkin.co m> wrote in message
        news:ckr59p$1ep o$1@dojima-n0.hi-ho.ne.jp...[color=blue]
        > 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>'.[/color]

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

        [and in followup][color=blue]
        > 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.[/color]

        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




        Comment

        • Michael Hoffman

          #5
          Re: get the name of lambda

          George Yoshida wrote:
          [color=blue]
          > 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.[/color]

          To be honest, I think munging the func_name attribute is a silly way to
          get what you want :)
          [color=blue]
          > I was doing something like:
          >
          > for func in (func1, func2, func3):
          > print func.func_name
          > print measure_ellapse d_time(func)[/color]

          How about:

          funcs = dict(func1=func 1, func2=func2, func3=lambda: None)
          for name, func in funcs.iteritems ():
          print name
          print measure_elapsed _time(func)
          --
          Michael Hoffman

          Comment

          • Piet van Oostrum

            #6
            Re: get the name of lambda

            >>>>> George Yoshida <ml@dynkin.co m> (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 <piet@cs.uu.n l>
            URL: http://www.cs.uu.nl/~piet [PGP]
            Private email: P.van.Oostrum@h ccnet.nl

            Comment

            • Tim Roberts

              #7
              Re: get the name of lambda

              George Yoshida <ml@dynkin.co m> wrote:
              [color=blue]
              >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__(o r foo.func_name) returns '<lambda>'.
              >I'm looking for a way to return 'foo', instead of '<lambda>'.[/color]

              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(zip py):
              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, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              Working...