我现在正在制作一款游戏,但我真的很讨厌在找到要修复的错误或新功能填补其他功能时需要几千行才能滚动。我将所有内容保存在一个主要类中,当我考虑将每个函数写入不同的文件时,我遇到了一个问题,我找不到任何内容
class game:
def __init__( self ):
self.foo = "Foo"
def function( self, bar ):
self.bar = bar
所以如果我们将它放入文件系统,它应该看起来像这样
游戏/
__初始化__。PY
function.py
我不知道如何将self或function传递给init或funtion。有某种 __括号 __ =(self,bar)代码可以帮助我,还是我还要继续滚动代码堆?
答案 0 :(得分:0)
我的评论错了,你可以按照你的描述去做,但是@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