【问题标题】:Can I call function which has 3 arguments, with only 2 of them我可以调用有 3 个参数的函数吗,只有 2 个参数
【发布时间】:2021-05-31 06:56:15
【问题描述】:

我对 python 很陌生,我必须根据时间和位置对波函数进行动画处理。该公式是 sin 和 cos 函数的和,取决于 n 和 m 整数。在公式中还有 x,我想将其作为变量保留并进行求和,然后有一个仅取决于 x 的波函数。有可能吗? 下面是总结的部分:

import sympy as sp
import numpy as np

n = sp.Symbol('n', integer = True, positive = True)
m = sp.Symbol('m', integer = True, positive = True)
x = sp.Symbol('x', real = True)
t = sp.Symbol('t', real = True, positive = True)
a = sp.Symbol('a', real = True, positive = True)
h = sp.Symbol('hbar', real = True, positive = True)
mass = sp.Symbol('m', real = True, positive = True)
V0 = sp.Symbol('V')

C = 4 * mass * a**2 * V0 / sp.pi**3 / h**2 
summ = sp.sin(m * sp.pi * x / a)/(n**2 - m**2)**2 * (n * sp.sin(sp.pi * m / 2) * sp.cos(sp.pi * n / 2) + m * sp.sin(sp.pi * n / 2) * sp.cos(sp.pi * m / 2))
Psi0 = lambdify((x, n, m), (summ).subs({h: 6.6 * 10**(-16), a: 1, mass: 9.1 * 10**(-31), V0: 10}))
Psi1 = 0
for i in range (0, 1000):
    for j in range (0, 1000):
        if i != j:
            Psi1 += C * Psi0 (x, i, j)

【问题讨论】:

标签: python parameter-passing


【解决方案1】:

不确定这是您需要的还是只是一个可选的关键字 arg,存在其他答案(尽管我会小心使用可选项,因为它们会导致更难检测的错误)。

如果是关于“渐进函数应用程序”,有一种方法可以在 Python 中使用部分应用程序来完成。假设你有函数fun(),它接受abc,你想首先只绑定ab,然后用不同的@值调用它987654327@。你可以这样做:

import functools


def three_args(a, b, c):
   print(f"Got {a}, {b} and {c}")


def main():
   # "fix" first two values by applying function partially with those
   fun = functools.partial(three_args, 1, 2)

   fun(3)   # prints "Got 1, 2 and 3" 

   fun(42)  # prints "Got 1, 2 and 42"

如果需要,当然第一个函数仍然可以使用所有三个参数调用。

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多