English 中文(简体)
持平时有多次不同论点[重复]。
原标题:Run python function multiple times with different arguments [duplicate]
  • 时间:2012-01-16 00:58:50
  •  标签:
  • python

然而,我需要履行一项职能,履行这一论点的职能,即每当评估其论点时,就有1,000次。 我有这样的情况:

def runner(f):
    def inner(*args):
        for i in xrange(1000):
            f(*args)
    return inner

但看来,如同这一条一样:runner(f)(random.randint(1,UPPER_BOUND)的援引有1,000次,有相同的论据。 如何正确?

最佳回答

您再次遇到的问题是,<代码> random.randint(1,UPPER_BOUND)在接通<代码>inner(<>inner(>功能>时,将立即进行评估。 你们需要的是将评价推迟到以后进行。

You could try something like this:

>>> def runner(f, callable):
...   def inner():
...     for i in xrange(1000):
...       f(*callable())
...   return inner
... 
>>> runner(f, lambda: (random.randint(1, 1000),))()
603
385
321
etc.

请注意,<编码>可唱名表决每次打上原有功能<代码>f。 还注意到<编码>可唱名表决必须交回序列号,如图或清单。

www.un.org/spanish/ga/president 你们可以这样做:

>>> def runner(f, callable):
...   def inner(*args, **kwds):
...     for i in xrange(1000):
...       pos = list(callable())
...       pos.extend(args)
...       f(*pos, **kwds)
...   return inner
... 
>>> def f(a, b, c, d = 3):
...   print a, b, c, d
... 
>>> runner(f, lambda: (random.randint(1,1000),))(3, 5, d = 7)
771 3 5 7
907 3 5 7
265 3 5 7
问题回答

你们需要改变随机计算。 职能定义:

例如,正如你开始的一样,@ is decorator syntax, 你可在here上读。 如果你不熟悉。 less然从另一个岗位上偷走ello子:

import random

UPPER_BOUND = 1000

def runner(fn):
    def wrapped():
        for i in range(0,10):
            stuff = random.randint(1,UPPER_BOUND)
            print(str(stuff) +  :   + fn())
    return wrapped

@runner
def hello():
    return  hello world 


if __name__== __main__ :
    hello()

Edit: also see here 理解你为何随意行事。 干.只有一次(在定义时间)执行,这就是为什么你的工作每次都有同样的论据。

页: 1 休息室内的电话:

def runner(function):
    def inner(callable, args=()):
        for i in xrange(1000):
            function(callable(*args))
    return inner

你们可以打电话给主人:

runner(f)(random.randint, args=(1, UPPER_BOND))

在我看来,你试图做些什么(也不涉及lam。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签