Python 中用于导入的默认参数

我有一个小模块写成:


心理测量学.py 的内容


def prob3pl(theta, D = 1.7, a, b, c): 

    result = c + (1 - c) / (1 + np.exp(-D * a * (theta - b)))

    return(result)


def gpcm(theta, d, score, a, D=1.7):

    Da = D * a

    result = np.exp(np.sum(Da * (theta - d[0:score]))) / np.sum(np.exp(np.cumsum(Da * (theta - d))))

    return(result)


if __name__ == '__main__':

    gpcm(theta, d, score, a, D=1.7)

    prob3pl(theta, D = 1.7, a, b, c)

现在使用 python 解释我执行以下操作:


import psychometrics as py

import numpy as np

py.prob3pl(0, a = 1, b= 0, c=0)

但是,在运行时会产生


>>> py.prob3pl(0,a=1,b=0,c=0)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: prob3pl() missing 1 required positional argument: 'D'

当我将函数复制并粘贴到解释器中时,它使用默认值运行,D = 1但在导入时不会发生这种情况。


我犯了什么错误,导致导入模块时无法识别 D 的默认值?


尚方宝剑之说
浏览 69回答 1
1回答

白板的微信

您的代码有语法错误 -SyntaxError:&nbsp;non-default&nbsp;argument&nbsp;follows&nbsp;default&nbsp;argument因此,请将函数 prob3pl() 更改为def&nbsp;prob3pl(theta,&nbsp;a,&nbsp;b,&nbsp;c,&nbsp;D&nbsp;=&nbsp;1.7):&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;c&nbsp;+&nbsp;(1&nbsp;-&nbsp;c)&nbsp;/&nbsp;(1&nbsp;+&nbsp;np.exp(-D&nbsp;*&nbsp;a&nbsp;*&nbsp;(theta&nbsp;-&nbsp;b)))&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(result)原因 - 在 python 函数声明中,任何默认参数之后都不应该有任何非默认参数。在这里 D=1.7
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python