Function 在类中的另一个函数内调用函数?

Function 在类中的另一个函数内调用函数?,function,class,python-3.x,Function,Class,Python 3.x,我一直在尝试调用类中另一个函数中的函数。这导致了一个错误。我能做些什么来解决这个问题。代码如下: class Goomba: def __init__(self,x,y): self.x = x self.y = y def goomleft(self,speed): for i in range(speed): if mask.get_at((self.x,self.y+10))[0] !=255:

我一直在尝试调用类中另一个函数中的函数。这导致了一个错误。我能做些什么来解决这个问题。代码如下:

class Goomba:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def goomleft(self,speed):
        for i in range(speed):
            if mask.get_at((self.x,self.y+10))[0] !=255:
                self.x-=1
    def goommove(self,direction):
        if direction == 'left':
            goomleft(self,3)  #this is where I called it
错误是
NameError:未定义全局名称“goomleft”

您必须调用彼此内部的函数,因为类中函数的范围不同:

class Goomba:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def goommove(self,direction):
        def goomleft(self,speed):
            for i in range(speed):
                if mask.get_at((self.x,self.y+10))[0] !=255:
                    self.x-=1
        if direction == 'left':
            goomleft(self,3)  #this is where I called it

沃特。。。这篇帖子让我得了癌症。