我现在正在做一个游戏,但是当我发现一个要修复的bug或者一个新的功能已经填充了另一个功能时,我真的讨厌有几千行要滚动。我把所有东西都保存在一个主类中,当我查看如何将每个函数写入一个不同的文件时,我遇到了一个问题,我什么也找不到
class game:
def __init__( self ):
self.foo = "Foo"
def function( self, bar ):
self.bar = bar
所以如果我们把它放到一个文件系统中,它应该是这样的
游戏/
我不知道如何将自我或功能传递到它本身或功能中。是否有某种__brackets __ =( self,条形码)代码可以帮助我,还是必须继续滚动大量代码?
发布于 2016-09-04 04:33:58
我的评论是错误的,你可以按照你的描述去做,但正如@Ignacio说的那样,它的代码设计并不好。这里有一个例子,因为我很好奇:
#Empty my_game 'class'
class my_game:
pass
#Function you can put in a different file and import here
#Will become set as the new init function to 'my_game'
def my_games_init(self,x,y):
self.x = x
self.y = y
#Assigning 'my_games_init' function to be used as init of 'my_game' class
my_game.__init__ = my_games_init
#Make a 'my_game' instance and show that it's x and y methods were set
g = my_game(1,2)
print g.x
print g.y
https://stackoverflow.com/questions/39313413
复制相似问题