Anonymus functions revisited

Ron radam2 at tampabay.rr.com
Tue Mar 22 12:13:47 EST 2005


On Tue, 22 Mar 2005 15:05:55 +0100, bruno modulix <onurb at xiludom.gro>
wrote:

>bruno modulix wrote:
>> Kay Schluehr wrote:
>> 
>>> Since George Sakkis proposed a new way of doing list comprehensions

>>> letting tuples-like objects (x,y,z=0) acting as functions on other
>>> tuples I wonder why this would not be a good starting point of
>>> rethinking anonymus functions?
>>>
>>> In Georges proposition the action is
>>>
>>>    (x,y,z=0) -> (x,y,z)


What about a safe exec as a replacement to lamba but witht he
flexability of exec in a safe and limeted way?  

safe_exec  (  (*inputs) , (expressionstring) ,  ( *outputs)  )

Functon to return a default value:
>> def dfv( arg = value):    
          return arg

Safe exec command:
>> x, y = 1, 2
>> safeexec  ( (x, y, dfv(z=0)), "# do nothing", ( x, y, z) ) 
(1, 2, 0)

What could we do, not do with this?



*   I actually started this reply here,  so below is how I got to the
above exression.


I'm trying to put my finger on the basic inconsistency here.  It has
something to do with the z=0 as a way to defining a default .  

Then there's the lamba which I hear may be removed,  but is difficult
to understand for beginners, and isn't readable in that the name
doesn't say what it does.   An alternative name, and possibly a
simpler syntax would be a plus.

Another concept that this touches is the indirect execution of an
expression.  Exec and eval do that, but then you introduce security
issues.

I'm wondering if there's a fundamental concept under these issues such
as a base function class or command that can evaluate the contents of
a tuple in a secure way?  

value = fn(arg, returnvalue) 

>> x = 1
>> fn( x, x*2)
2

Use lists or tuples for multiple arguments and return expressions:

>> x, y, z = 1, 2, 3
>> fn( (x,y,z=0), (x,y,z) ) 
(1, 2, 3)

But it's that "z=0" causes problems here.   In a tuple it's equivalent
to saying 1=2.   or 'a'='b'

So we need a way to say 'if name is undefined, bind it to object'.

# function to give a default value
def dfv( arg = value):    
    return arg

>> x, y = 1, 2
>> fn( (x, y, dfv(x=0)), ( x, y, z ))
(1, 2, 3)

Now since this could execute in it's own private space, it might also
offer a way to use exec or eval() indirectly in a very limited and
safe way.  

>> estring = \
""" 
result = ''
data = 'abcdefghigklmnop'
for ch in data:
    if ch != filterc:
        result.join(ch)
"""
>> fn( ( dfv(filterc='d'), evalstring), (exec estring) )

So we need to use a three item tuple:

safe_exec  (  (*inputs) , (expressionstring) ,  ( *outputs)  )

>> def dfv( arg = value):    
         return arg
>> x, y = 1, 2
>> safeexec( (x, y, dfv(z=0)), "# do nothing", ( x, y, z) ) 
(1, 2, 0)
 
Long way around to here,  but is this something that has potential?
It would have to be a built in to ensure it's safe to use.   But with
an exec string,  it can possibly do a lot more than lamba, and it
probably wouldn't be as fast.  But the flexibility could be useful.

Ron










More information about the Python-list mailing list