By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,568 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,568 IT Pros & Developers. It's quick & easy.

block/lambda

P: n/a
iu2
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

Jul 28 '08 #1
Share this Question
Share on Google+
7 Replies

P: n/a
iu2
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.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...
Jul 28 '08 #2

P: n/a
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.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.
Jul 29 '08 #3

P: n/a
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
Jul 29 '08 #4

P: n/a
iu2 <is*****@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
Jul 29 '08 #5

P: n/a
iu2
On Jul 29, 9:36*am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
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.blogspot.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
Jul 29 '08 #6

P: n/a
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
Jul 29 '08 #7

P: n/a


iu2 wrote:
On Jul 29, 9:36 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
>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

Jul 29 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.