0

I'm working on a game as a python learning project, and I'm trying to move away from having a single file. What is the best way to parse functions out so they can still access important objects? For example, I have an object that basically controls the window and calculates the sizes of ui elements when the user resizes the window. I'd like to be able to use the instance of this object without having to pass it into every function through an argument. An example:

function.py

import test

def add_foo():

    newfoo.add()

test.py

import function

class foo:
    def __init__(self):
        self.x = 1

    def add(self):
        self.x += 10

newfoo = foo()

if __name__ == '__main__':

    newfoo.add()
    print newfoo.x

    function.add_foo()
    print newfoo.x  

Am I just thinking about this the wrong way? The obvious solution here is to add an argument to function.add_foo() that lets me pass in the newfoo instance which lets me use it. But I find this to be a pretty cumbersome way to deal with an object that essentially needs to be universally accessible to every module and function if I call it since it changes anytime the user resizes the window.

It's also possible I should be structuring my programs in a different way entirely.

  • Have you considered creating a global object for "an object that essentially needs to be universally accessible to ever module and function if I call"? – boardrider Apr 22 '17 at 13:23
  • I actually did this very thing after answer the question that managed to solve the problem after googling with some better key words. I'll link to that stack overflow answer when I answer the question with my solution. – Michael Coates Apr 23 '17 at 14:14

0 Answers0