简体   繁体   中英

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. But deleting it is also wrong....It show psi() takes 2 positional arguments but 3 were given .

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

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

second value of function: x

third value of function: self.vi

and your function value is finished after that you send "si" this will fourth value and program output:

3 positional arguments but 4 were given.

What you have to do is make the number of values ​​the psi function will take:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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