通过*或**将参数传递给函数

时间:2020-03-03 10:50:14

标签: python packing

(说)有两个函数,即规范函数和统一函数,它们都采用scale和loc参数。 我想要一个名为pick_a_distribution的函数,该函数选择要选择的发行版,然后分配适当的参数并执行其他一些操作。但是我也希望用户能够为一个参数定义(说)一个值并仅传递该值,而不必定义整个参数列表。

如果我传递了完整的参数列表,我就设法编写了一些有效的代码(因此,对于我不想传递的参数,我需要知道默认值是什么)。

有没有办法提高效率(在上面的示例中,有没有办法只传递'loc'参数)?

下面是一些有效的代码。 。 。

from scipy.stats import norm, genlogistic, uniform, expon
import math

distribution = {'1': 'normal', '2': 'uniform', '3': 'genlogistic', '4': 'exponential', '5': 'error'}
def mydistr(distr,numofsims, myargument):
    switcher = {
        '1': norm(*myargument),
        '2': uniform(*myargument),
        '3': genlogistic(*myargument),
        '4': expon(*myargument)
    }
    myobs = switcher.get(distr, 'Error in getting distribution')
    return myobs.rvs(numofsims)


print(mydistr( myargument = [2,80], numofsims = 100000, distr = '2' ).mean())

,还有一些没有。 。 。 (与以前相同的进口商品)

    def mydistr(distr,numofsims, myargument):
        switcher = {
            '1': norm(*myargument),
            '2': uniform(**myargument),
            '3': genlogistic(*myargument),
            '4': expon(*myargument)
        }
        myobs = switcher.get(distr, 'Error in getting distribution')
        return myobs.rvs(numofsims)

    print(mydistr( myargument = {loc:80}, numofsims = 100000, distr = '2' ).mean())

在最后的通话中,我也尝试过以下说法: =({loc:80}),=(loc,80)并尝试使用“ loc”;

我得到的名字'loc'没有定义,当我把它放在''中时,我得到一些比较错误。

任何评论都非常感谢,在此先感谢您:)

0 个答案:

没有答案