Python OO——成员函数定义和关键字 self

发布于 2025-01-07 22:17:43 字数 791 浏览 0 评论 0原文

我正在用这个构造函数编写一个Python类:

      #constuctor
def __init__(self, initPt_=[1,1],fun_=Optim_tests.peaks,NITER_=30,alpha_=0.7,NMAX_=5000,FTOL_=10**(-10)):
    self.initPt = initPt_
    self.fun = fun_
    self.alpha = alpha_
    self.ITER = NITER_
    self.NMAX = NMAX_
    self.FTOL = FTOL_

并定义两个成员函数:

def buildSimplex(self):
    self.simplex=[]
    self.simplex.append([x for x in self.initPt])
    for i in range(len(self.initPt)):
        temp=[x for x in self.initPt]
        temp[i]=self.initPt[i]+1
        self.simplex.append(temp)
    self.npts=len(self.simplex)

def sA(self):
    self.buildSimplex()

调用第二个函数时,会发生错误:

NameError: global name 'buildSimplex' is not defined    

你有线索吗?

I am writting a Python class with this constructor:

      #constuctor
def __init__(self, initPt_=[1,1],fun_=Optim_tests.peaks,NITER_=30,alpha_=0.7,NMAX_=5000,FTOL_=10**(-10)):
    self.initPt = initPt_
    self.fun = fun_
    self.alpha = alpha_
    self.ITER = NITER_
    self.NMAX = NMAX_
    self.FTOL = FTOL_

and defining both member functions:

def buildSimplex(self):
    self.simplex=[]
    self.simplex.append([x for x in self.initPt])
    for i in range(len(self.initPt)):
        temp=[x for x in self.initPt]
        temp[i]=self.initPt[i]+1
        self.simplex.append(temp)
    self.npts=len(self.simplex)

def sA(self):
    self.buildSimplex()

When calling second functions, error happens:

NameError: global name 'buildSimplex' is not defined    

Do you have a clue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2025-01-14 22:17:43

乍一看,我会说这是一个识别问题,但您需要提供实际代码以获得更具体的答案。

我之所以这样说是因为您遇到了错误。如果您正确声明了您的类,并尝试调用未定义的实例的方法,您实际上应该得到一个:AttributeError:实例没有属性'xxxx'。如果方法是在类中声明的,则无需关心定义方法的顺序。请参阅下面的 met1met4 示例

例如:

class A():
   def met1(self):
      print self.met4()

   def met2(self):
      self.met3()

   def met4():
      print 'x'


 a = A()
 a.met1()
 >>> x
 a.met2()
 >>> AttributeError: A instance has no attribute 'met3'

At first sight I would say it's a identation problem, but you need to provide the actual code for a more specific answer.

The reason I'm saying this is because of the error you're getting. If you declared your class properly, and try to call a method of an instance that is not define, you should actually get a: AttributeError: A instance has no attribute 'xxxx'. And you don't need to care about the order you define your methods if they are declared in a class. See the e xample of met1 and met4 below

For example:

class A():
   def met1(self):
      print self.met4()

   def met2(self):
      self.met3()

   def met4():
      print 'x'


 a = A()
 a.met1()
 >>> x
 a.met2()
 >>> AttributeError: A instance has no attribute 'met3'
牵你的手,一向走下去 2025-01-14 22:17:43

您的错误 NameError: global name 'buildTool1' is not Defined 表示您正在尝试访问变量 buildTool1 但其未在本地或全局中定义。

请检查这个

class test(object):

    def __init__(self, name):
        self.name = name

    def buildSimplex(self):
        print "CALL"

    def sA(self):
        self.buildSimplex()


if __name__ == '__main__':
    x = test('test')
    x.sA()

Your error NameError: global name 'buildTool1' is not defined says you are trying to access the variable buildTool1 buts its not define in local or global.

Please check this

class test(object):

    def __init__(self, name):
        self.name = name

    def buildSimplex(self):
        print "CALL"

    def sA(self):
        self.buildSimplex()


if __name__ == '__main__':
    x = test('test')
    x.sA()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文