block/lambda

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

    block/lambda

    Hi,

    Playing with imitating lambdas and ruby blocks in Python, I came up
    with a very simple construct, for example:

    import compiler

    def dotimes(i, code):
    for i in range(i):
    exec code

    dotimes(5, '''
    for j in range(i):
    print j,
    print
    ''', '<string>', 'exec')

    This will print
    0
    0 1
    0 1 2
    0 1 2 3

    A more efficient code would probably be

    dotimes(5, compiler.compil e('''
    for j in range(i):
    print j,
    print
    ''', '<string>', 'exec'))

    which is, to my understanding, exactly what a ruby block is.

    But the actual "discovery" here, is that the triple quote - ''' -
    makes a syntax for block passing. Having a code editor that keeps
    colorizing what's inside the quotes like a normal code would make it
    easier to maintain.

    Is it possible to grant Python another syntactic mark, similar to
    triple quotes, that will actually make the enclosed code a compiled
    code, or an anonymous function?

    I know that anonymous functions (long lambdas...) are not on the road
    map. But I ask this because, as I understand it, the triple quote
    actually presents a syntax for it.
    Isn't it actually a matter of taking the triple-quotes a little bit
    further?

    Thanks

  • iu2

    #2
    Re: block/lambda

    On Jul 28, 10:06 pm, iu2 <isra...@elbit. co.ilwrote:
    Hi,
    >
    Playing with imitating lambdas and ruby blocks in Python, I came up
    with a very simple construct, for example:
    >
    import compiler
    >
    def dotimes(i, code):
        for i in range(i):
            exec code
    >
    dotimes(5, '''
    for j in range(i):
            print j,
    print
    ''', '<string>', 'exec')
    >
    This will print
    0
    0 1
    0 1 2
    0 1 2 3
    >
    A more efficient code would probably be
    >
    dotimes(5, compiler.compil e('''
    for j in range(i):
            print j,
    print
    ''', '<string>', 'exec'))
    >
    which is, to my understanding, exactly what a ruby block is.
    >
    But the actual "discovery" here, is that the triple quote - ''' -
    makes a syntax for block passing. Having a code editor that keeps
    colorizing what's inside the quotes like a normal code would make it
    easier to maintain.
    >
    Is it possible to grant Python another syntactic mark, similar to
    triple quotes, that will actually make the enclosed code a compiled
    code, or an anonymous function?
    >
    I know that anonymous functions (long lambdas...) are not on the road
    map. But I ask this because, as I understand it, the triple quote
    actually presents a syntax for it.
    Isn't it actually a matter of taking the triple-quotes a little bit
    further?
    >
    Thanks
    There is a mistake in my first example, the code is, of course:
    dotimes(5, '''
    for j in range(i):
    print j,
    print
    ''')

    Sorry...

    Comment

    • castironpi

      #3
      Re: block/lambda

      On Jul 28, 3:12 pm, iu2 <isra...@elbit. co.ilwrote:
      On Jul 28, 10:06 pm, iu2 <isra...@elbit. co.ilwrote:
      >
      >
      >
      Hi,
      >
      Playing with imitating lambdas and ruby blocks in Python, I came up
      with a very simple construct, for example:
      >
      import compiler
      >
      def dotimes(i, code):
          for i in range(i):
              exec code
      >
      dotimes(5, '''
      for j in range(i):
              print j,
      print
      ''', '<string>', 'exec')
      >
      This will print
      0
      0 1
      0 1 2
      0 1 2 3
      >
      A more efficient code would probably be
      >
      dotimes(5, compiler.compil e('''
      for j in range(i):
              print j,
      print
      ''', '<string>', 'exec'))
      >
      which is, to my understanding, exactly what a ruby block is.
      >
      But the actual "discovery" here, is that the triple quote - ''' -
      makes a syntax for block passing. Having a code editor that keeps
      colorizing what's inside the quotes like a normal code would make it
      easier to maintain.
      >
      Is it possible to grant Python another syntactic mark, similar to
      triple quotes, that will actually make the enclosed code a compiled
      code, or an anonymous function?
      >
      I know that anonymous functions (long lambdas...) are not on the road
      map. But I ask this because, as I understand it, the triple quote
      actually presents a syntax for it.
      Isn't it actually a matter of taking the triple-quotes a little bit
      further?
      >
      Thanks
      >
      There is a mistake in my first example, the code is, of course:
      dotimes(5, '''
      for j in range(i):
              print j,
      print
      ''')
      >
      Sorry...
      You could do

      code= '''#
      for _ in range( 2 ):
      pass
      '''

      and signify the code block to your editor with the leading blank
      comment.

      Comment

      • John Nagle

        #4
        Re: block/lambda

        iu2 wrote:
        Hi,
        >
        Playing with imitating lambdas and ruby blocks in Python, I came up
        with a very simple construct, for example:
        >
        import compiler
        Python supports nested functions. You don't have to use a lambda
        form just to get a local function. Just write an ordinary nested
        def within another def.

        John Nagle

        Comment

        • Duncan Booth

          #5
          Re: block/lambda

          iu2 <israelu@elbit. co.ilwrote:
          Is it possible to grant Python another syntactic mark, similar to
          triple quotes, that will actually make the enclosed code a compiled
          code, or an anonymous function?
          >
          Yes, the syntactic mark you are looking for is 'def'.

          Your example becomes:
          >>def dotimes(n, callable):
          for i in range(n): callable(i)

          >>def block(i):
          for j in range(i):
          print j,
          print

          >>dotimes(5, block)
          0
          0 1
          0 1 2
          0 1 2 3

          The only thing you asked for that this doesn't do is make 'block'
          anonymous, but actually that is a good thing.


          --
          Duncan Booth http://kupuguy.blogspot.com

          Comment

          • iu2

            #6
            Re: block/lambda

            On Jul 29, 9:36 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
            iu2 <isra...@elbit. co.ilwrote:
            Is it possible to grant Python another syntactic mark, similar to
            triple quotes, that will actually make the enclosed code a compiled
            code, or an anonymous function?
            >
            Yes, the syntactic mark you are looking for is 'def'.
            >
            Your example becomes:
            >
            >def dotimes(n, callable):
            >
                    for i in range(n): callable(i)
            >
            >def block(i):
            >
                    for j in range(i):
                            print j,
                    print
            >
            >dotimes(5, block)
            >
            0
            0 1
            0 1 2
            0 1 2 3
            >
            The only thing you asked for that this doesn't do is make 'block'
            anonymous, but actually that is a good thing.
            >
            --
            Duncan Boothhttp://kupuguy.blogspo t.com
            Ok, I've got 2 questions about it:

            1. Can you please explain why it is a good thing?
            2. Will it be possible in Python 3.0 to do the following:
            >>def dotimes(n, callable):
            for i in range(n): callable()
            >>def block():
            nonlocal i
            for j in range(i):
            print j,
            print

            Thanks

            Comment

            • jiri.zahradil@gmail.com

              #7
              Re: block/lambda

              2. Will it be possible in Python 3.0 to do the following:
              >
              >def dotimes(n, callable):
              >
                      for i in range(n): callable()
              >
              >def block():
              >
                      nonlocal i
                      for j in range(i):
                              print j,
                      print
              dotimes seems ok and what is wrong with that function "block"? You do
              not need to specify that i is "nonlocal", global i will be used.
              >>i=10
              >>def block():
              for j in range(i):
              print j,
              print
              >>block()
              0 1 2 3 4 5 6 7 8 9

              Jiri

              Comment

              • Terry Reedy

                #8
                Re: block/lambda



                iu2 wrote:
                On Jul 29, 9:36 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
                >Your example becomes:
                >>
                >>>>def dotimes(n, callable):
                > for i in range(n): callable(i)
                >>
                >>>>def block(i):
                > for j in range(i):
                > print j,
                > print
                >>
                >>>>dotimes(5 , block)
                >0
                >0 1
                >0 1 2
                >0 1 2 3
                >>
                >The only thing you asked for that this doesn't do is make 'block'
                >anonymous, but actually that is a good thing.
                >
                Ok, I've got 2 questions about it:
                >
                1. Can you please explain why it is a good thing?
                All functions defined with lambda expressions get the pseudoname
                '<lambda>'. All functions defined with def get the name you give it,
                which is typically unique within some scope. The representation of a
                function, whether intentionally printed or printed as part of a
                traceback, is more meaningful with a specific name than a general name.

                2. Will it be possible in Python 3.0 to do the following:
                >
                >>>def dotimes(n, callable):
                for i in range(n): callable()
                >
                >>>def block():
                nonlocal i
                for j in range(i):
                print j,
                print
                If you indent block so it is a nested function, yes. But the nonlocal
                declaration is not needed unless you rebind 'i' from within the nested
                function, which block does not do.

                tjr

                Comment

                Working...