Maximillian Wadkins Maximillian Wadkins - 3 months ago 9
Python Question

__init__() compared to __init__.py

I am working on a game right now but I really hate having a few thousand lines to scroll through when finding a bug to fix or a new feature has stuffed up another function. I have kept everything in one main class and when I looked into writing each function into a different file, I had one problem, I couldn't find anything

class game:
def __init__( self ):
self.foo = "Foo"
def function( self, bar ):
self.bar = bar


so if we put it into a file system, it should look something like this

game/


  • __init__.py

  • function.py



and I do not know how to pass self or function into either init or funtion. Is there some sort of
__brackets
__ = ( self, bar ) code that will help me, or will I continue to have to scroll through heaps of code?

Answer

I was wrong in my comment, you can do as you describe, but as @Ignacio stated its not good code design. Here's an example anyway because I was curious:

#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