Code blocks as parameters

James_Althoff at i2.com James_Althoff at i2.com
Tue May 1 15:51:02 EDT 2001


Greg writes:
>For a while I've been thinking about making something like
>
>  func(arg1,arg2):
>    statements
>
>mean the same as (except for the local function name):
>
>  def thunk():
>    statements
>  func(thunk,arg1,arg2)
>
>But then someone is going to want to pass arguments
>to the thunk, and I haven't thought of a good
>syntax for that yet.
>
>--
>Greg Ewing, Computer Science Dept, University of Canterbury,

perhaps something might be possible along the lines of:

func(arg1,arg2) targ1,targ2:
    statements using targ1 and/or targ2

which would turn into:

def thunk(targ1,targ2):
    statements using targ1 and/or targ2

e.g.,

class MyList:
    def __init__(self,alist):
        self.list = alist
    def do(self,function):
        for member in self.list:
            function(member)

mylist = MyList([1,2,3,4,5])

----

mylist.do() item:
    print item

turns into:

def thunk(item):
    print item
mylist.do(thunk)

and e.g., without args:

form = Form()
form.setSearchString('black pen')

window.showWaitCursorDuring():
    form.executeQuery()
    form.displayResults()


jim







More information about the Python-list mailing list