block/lambda

I

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
 
I

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

There is a mistake in my first example, the code is, of course:
dotimes(5, '''
for j in range(i):
print j,
print
''')

Sorry...
 
C

castironpi

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.
 
J

John Nagle

iu2 said:
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
 
I

iu2

Yes, the syntactic mark you are looking for is 'def'.

Your example becomes:


        for i in range(n): callable(i)


        for j in range(i):
                print j,
        print


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?
2. Will it be possible in Python 3.0 to do the following:
for i in range(n): callable()
nonlocal i
for j in range(i):
print j,
print

Thanks
 
J

jiri.zahradil

2. Will it be possible in Python 3.0 to do the following:


        for i in range(n): callable()


        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.
for j in range(i):
print j,
print0 1 2 3 4 5 6 7 8 9

Jiri
 
T

Terry Reedy

iu2 said:
for i in range(n): callable(i)

for j in range(i):
print j,
print

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.
[/QUOTE]
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:

for i in range(n): callable()

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top