Argument of the bool function

T

Thomas Rachel

Am 25.04.2011 16:29, schrieb Thomas Rachel:
or maybe even better (taking care for closures):

function = bool
value = 'the well at the end of the world'
## ...
actions.append(lambda val=value: function(val))
## ...
for function in actions:
results.append(function())

Or yet even better:

class Job(object):
def __init__(self, target, *args, **kwargs):
self.target = lambda: target(*args, **kwargs)
def __call__(self):
return self.target()

in order to do

actions.append(Job(function, val))
actions.append(Job(function, x=val))

and then (thanks, Chris...)

results = [function() for function in actions]


or maybe (additionally or alternatively)

class ActionQueue(list):
def addJob(self, target, *args, **kwargs):
self.append(lambda: target(*args, **kwargs))
def __call__(self):
if 0: # first thought
for job in self:
yield job()
else: # second thought - clean up...
while self:
job = self.pop(0)
yield job()

with

actions = ActionQueue()

actions.addJob(function, val)
actions.addJob(function, x=val)

results = list(actions()) # for collecting them and having them at once
# with generator, all call results can as well be emitted as soon as
they are available - depending what shall be done with the results


mmm... too much imagination I think... ;-)


Thomas
 
I

Ian Kelly

Am 25.04.2011 16:29, schrieb Thomas Rachel:


Or yet even better:

class Job(object):
   def __init__(self, target, *args, **kwargs):
       self.target = lambda: target(*args, **kwargs)
   def __call__(self):
       return self.target()

in order to do

actions.append(Job(function, val))
actions.append(Job(function, x=val))

from functools import partial
actions.append(partial(function, val))
actions.append(partial(function, x=val))

Cheers,
Ian
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top