Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4002

Re: Argument of the bool function

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: Argument of the bool function
Date 2011-04-25 23:28 +0200
Organization A newly installed InterNetNews server
Message-ID <ip4p1v$gmf$1@r03.glglgl.eu> (permalink)
References (1 earlier) <mailman.154.1302280920.9059.python-list@python.org> <4da1a8b5$0$23679$426a74cc@news.free.fr> <mailman.190.1302440567.9059.python-list@python.org> <inslea$p6d$1@speranza.aioe.org> <ip40gf$i0o$1@r03.glglgl.eu>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Argument of the bool function Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-25 16:29 +0200
  Re: Argument of the bool function Chris Angelico <rosuav@gmail.com> - 2011-04-26 05:33 +1000
    Re: Argument of the bool function Paul Rubin <no.email@nospam.invalid> - 2011-04-25 16:26 -0700
      Re: Argument of the bool function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-26 02:38 +0000
  Re: Argument of the bool function Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-25 23:28 +0200
    Re: Argument of the bool function Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-25 17:44 -0600

csiph-web