简体   繁体   English

为什么 psi() 需要 3 个位置参数但给出了 4 个?

[英]Why psi() takes 3 positional arguments but 4 were given?

Emm,I know the similiar problems are asked many times in stackoverflow, but them don't work.... And I guess the self is twice defined.嗯,我知道类似的问题在 stackoverflow 中被问了很多次,但它们不起作用......而且我猜自我是两次定义的。 But deleting it is also wrong....It show psi() takes 2 positional arguments but 3 were given .但是删除它也是错误的......它显示psi() 需要 2 个位置参数,但给出了 3 个

So I ask someone for help....所以我请人帮忙....

# -*- coding: utf-8 -*-
import math
import numpy as np
import matplotlib as plt

class HT(object):
    def __init__(self,vi=1.0,w=1.0,wave_number=101,dot_number=101,r=10.0):
        self.vi=vi
        self.w=w
        self.wave_number=wave_number
        self.dot_number=dot_number
        self.r=r
        self.H=np.zeros((wave_number,wave_number))
        for i in range(wave_number):
            for j in range(wave_number):
                self.H[i,j]=self.H_mn(i,j)

    def psi(self,x,si):
        return math.sqrt(self.vi/math.pi)*math.exp(-self.vi*(x-si)**2)

    def psi2(self,x,si):
        return self.psi(x,self.vi,si)*(2.0*self.vi*(-1.0+2.0*self.vi*(x-si)**2))

    def T_x(self,x,m,n):
        h=2.0*self.r/(self.wave_number-1.0)
        return -0.5*self.psi(x,-self.r+m*h)*self.psi2(x,-self.r+n*h)

    def V_x(self,x,m,n):
        h=2.0*self.r/(self.wave_number-1.0)
        return self.psi(x,-self.r+m*h)*0.5*self.w**2.0*x**2*self.psi(x,-self.r+n*h)

    def T_mn(self,m,n):
        T_mn=0.0
        hp=2.0*self.r/(self.dot_number)
        for i in range(self.dot_number):
            T_mn=T_mn+hp/3.0*(self.T_x(-self.r+2.0*i*hp,m,n)\
                              +4.0*self.T_x(-self.r+(2.0*i+1.0)*hp,m,n)\
                                 +self.T_x(-self.r+(2.0*i+2.0)*hp,m,n))
        return T_mn
    
    def V_mn(self,m,n):
        V_mn=0.0
        hp=2.0*self.r/(self.dot_number)
        for i in range(self.dot_number):
            V_mn=V_mn+hp/3.0*(self.V_x(-self.r+2.0*i*hp,m,n)\
                              +4.0*self.V_x(-self.r+(2.0*i+1.0)*hp,m,n)\
                                 +self.V_x(-self.r+(2.0*i+2.0)*hp,m,n))
        return V_mn
    
    def H_mn(self,m,n):
        return self.V_mn(m,n)+self.T_mn(m,n)
    
myht=HT()

Indicates that the self is the subject of the class no need to call a selfie again so your function can only have 2 variables表示 self 是类的主题,无需再次调用自拍,因此您的函数只能有 2 个变量

your function :你的功能:

def psi(self,x,si):
    return math.sqrt(self.vi/math.pi)*math.exp(-self.vi*(x-si)**2)

if you try to call it like this:如果您尝试这样称呼它:

self.psi(x,self.vi,si)

first value of function: self函数的第一个值:self

second value of function: x函数的第二个值:x

third value of function: self.vi函数的第三个值:self.vi

and your function value is finished after that you send "si" this will fourth value and program output:并且您的函数值在您发送“si”之后完成,这将是第四个值和程序输出:

3 positional arguments but 4 were given.

What you have to do is make the number of values ​​the psi function will take:您需要做的是使 psi 函数将采用的值的数量:

def psi(self,x,si,other):
    .......

or specifying less value when calling the function, like this:或在调用函数时指定较小的值,如下所示:

self.psi(x,self.vi)

or:或者:

self.psi(self.vi,si)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM