Python 如何在define函数中放置define

Python 如何在define函数中放置define,python,pygame,function,Python,Pygame,Function,我试图弄清楚如何将define函数放在类的define函数中。我想做的Python代码: class Guy(): def move(self): def left(self): //compute def right(self): //compute def up(self): //compute def down(self):

我试图弄清楚如何将define函数放在类的define函数中。我想做的Python代码:

class Guy():
    def move(self):
        def left(self):
            //compute
        def right(self):
            //compute
        def up(self):
            //compute
        def down(self):
            //compute

然后就可以在节目稍后打电话给Guy.move。解释器给我一个错误,说“函数没有正确的属性”或类似的东西。有没有办法做到这一点,而不必将我的函数从move函数中带出来,并称它们为Guy.right、Guy.left等等

这是一种糟糕的编码方式

在Guy类下无法调用left函数,因为它在move函数的范围内,并且与类不同,在另一个函数中添加函数不会使其成为属性

在内部函数成为外部函数的属性之前,无法使用封闭函数的名称调用它

万一

def move():
    def left():
i、 e.如果一个函数中只有一个函数,则可以从移动函数返回左侧

但是,在这种情况下,移动函数范围内有多个函数


您应该寻找类似嵌套类的东西,并将其作为类而不是函数进行移动

这是一种糟糕的编码方式

在Guy类下无法调用left函数,因为它在move函数的范围内,并且与类不同,在另一个函数中添加函数不会使其成为属性

在内部函数成为外部函数的属性之前,无法使用封闭函数的名称调用它

万一

def move():
    def left():
i、 e.如果一个函数中只有一个函数,则可以从移动函数返回左侧

但是,在这种情况下,移动函数范围内有多个函数


您应该寻找类似嵌套类的东西,并将其作为类而不是函数移动

我会避免嵌套方法和函数

每次调用move方法时,都会重新编译这些定向方法,并且只能从move中调用这些定向方法

也许您需要一个通用的move方法,该方法将方向参数与self一起接受

def move(self, direction):
    if direction == "left":
        pass //do something
    //etc

我会避免嵌套方法和函数

每次调用move方法时,都会重新编译这些定向方法,并且只能从move中调用这些定向方法

也许您需要一个通用的move方法,该方法将方向参数与self一起接受

def move(self, direction):
    if direction == "left":
        pass //do something
    //etc

函数通常没有向外部作用域公开的字段和其他函数。这通常是对象的工作。但是你可以用一些像

class Guy():
    class Move():
        def __init__(self):
            self.whatevs = "whatevs"
        def left(self):
            print("left")
            pass
            #compute
        def right(self):
            print("right")
            pass
            #compute
        def up(self):
            print("up")
            pass
            #compute
        def down(self):
            print("down")
            pass
            #compute

    def __init__(self):
        self.move = self.Move()

guy = Guy()
guy.move.left()

我很确定这不是一个好的设计。你为什么要做盖伊,走,走?一些优雅的想法,因为它不是在其他方面,比如在简单方面。

函数通常没有字段和其他函数,它们暴露在外部作用域中。这通常是对象的工作。但是你可以用一些像

class Guy():
    class Move():
        def __init__(self):
            self.whatevs = "whatevs"
        def left(self):
            print("left")
            pass
            #compute
        def right(self):
            print("right")
            pass
            #compute
        def up(self):
            print("up")
            pass
            #compute
        def down(self):
            print("down")
            pass
            #compute

    def __init__(self):
        self.move = self.Move()

guy = Guy()
guy.move.left()
class Guy():
    def move(self,dir):
       def left():
          print "left!"
       {'left':left}[dir]()

Guy().move("left")
我很确定这不是一个好的设计。你为什么要做盖伊,走,走?一些优雅的想法,因为它不是在其他方面,比如在简单方面

class Guy():
    def move(self,dir):
       def left():
          print "left!"
       {'left':left}[dir]()

Guy().move("left")
将工作,虽然我没有看到太多的用例为它


虽然我看不到太多的用例,但它仍然可以工作…

有一种方法可以实现类似于原始代码要求的功能: Guy.move将调用move函数,Guy.move.rightGuy将调用move.right函数。唯一的区别是我必须传递self对象才能显式地移动.right

我不鼓励任何人走这条路,更像是“python能做什么”的事情

def to_attr(f):
    import inspect
    import new
    for c in f.func_code.co_consts:
        if inspect.iscode(c):
            g = new.function(c, globals())
            setattr(f, g.func_name, g)    

    return f

class Guy(object):
    @to_attr
    def move(self):
        def left(self):
            pass
        def right(self):
            print "Right"
        def up(self):
            pass
        def down(self):
            pass
        print "Move"

Guy.move.right(Guy())
Guy().move()

检查代码借用自

有一种方法可以实现与原始代码要求类似的功能: Guy.move将调用move函数,Guy.move.rightGuy将调用move.right函数。唯一的区别是我必须传递self对象才能显式地移动.right

我不鼓励任何人走这条路,更像是“python能做什么”的事情

def to_attr(f):
    import inspect
    import new
    for c in f.func_code.co_consts:
        if inspect.iscode(c):
            g = new.function(c, globals())
            setattr(f, g.func_name, g)    

    return f

class Guy(object):
    @to_attr
    def move(self):
        def left(self):
            pass
        def right(self):
            print "Right"
        def up(self):
            pass
        def down(self):
            pass
        print "Move"

Guy.move.right(Guy())
Guy().move()

检查代码是从

借来的检查此stackoverflow post:检查此stackoverflow post:是否有任何方法使函数成为包含它的函数的属性?@EliasBenevedes:我建议您移动到嵌套类方法,但如果您仍想继续此方法,你可以看看有没有办法让函数成为包含它的函数的属性?@EliasBenevedes:我建议你转向嵌套类方法,但如果你仍然想继续使用这种方法,你可以看看