为了账号安全,请及时绑定邮箱和手机立即绑定

Python 中用于导入的默认参数

Python 中用于导入的默认参数

尚方宝剑之说 2022-10-25 16:06:26
我有一个小模块写成:心理测量学.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 pyimport numpy as nppy.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 的默认值?
查看完整描述

1 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

您的代码有语法错误 -

SyntaxError: non-default argument follows default argument

因此,请将函数 prob3pl() 更改为

def prob3pl(theta, a, b, c, D = 1.7): 
    result = c + (1 - c) / (1 + np.exp(-D * a * (theta - b))) 
       return(result)

原因 - 在 python 函数声明中,任何默认参数之后都不应该有任何非默认参数。在这里 D=1.7


查看完整回答
反对 回复 2022-10-25
  • 1 回答
  • 0 关注
  • 65 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信