On Jul 28, 3:12 pm, iu2 <[EMAIL PROTECTED]> wrote: > On Jul 28, 10:06 pm, iu2 <[EMAIL PROTECTED]> wrote: > > > > > 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.compile(''' > > 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. -- http://mail.python.org/mailman/listinfo/python-list