I am trying to figure out how to put a define function inside a define function inside of a class. Python code of what I want to do:
class Guy():
def move(self):
def left(self):
//compute
def right(self):
//compute
def up(self):
//compute
def down(self):
//compute
And then be able to call Guy.move.right() later on in the program. The interpreter gives me an error saying ‘Function has no attribute right’ or something like that. Is there any way to do this without having to bring my functions out of the move function and calling them Guy.right, Guy.left, etc.?
this is a bad way to code…
there is no way to call
leftfunction underGuy classsince it is in the scope ofmovefunction, and unlikeclass, adding a function inside another function does not make it its attribute.Until the inside function is a attribute of the outside function there is no way to call it using the name of the enclosing function
In case
i.e. If there is only one function inside a function it is feasible to return
left()frommovefunction.However in this case there are multiple functions inside
movefunction scope.You should look for something like a
nested classand makemoveas aclassrather than a function