Reputation: 543
I'm relatively new to python and oop, and i have a question around the design of my code for a hobby project.
I created a lot of variables in my main program. These variables are lists of objects (not configuration parameters and not constants). The objects in the lists are sprites.
I'm passing these variables around between objects, by calling methods and passing the variables around as arguments for a specific method. (pass-by-reference)
For example:
spritelist = [Sprite(...), Sprite(..)]
mycollisiondetector = CollisionDetector()
mycollisiondetector.check_collision(spritelist)
Then, in class CollisionDetector, spritelist is passed to "private" methods of the class. These private methods call other methods, and keep passing spritelist ... .
So, my question is just this: is there an alternative for endlessly passing variables around from one method to another ?
Upvotes: 1
Views: 256
Reputation: 23322
If you're dealing with instance variables (not configuration constants), it's considered bad practice to separate the variables into a module (a different file), since you're mixing instance state and global state.
If you have many references being passed around repeatedly, this is usually a indicator of bad class hierarchy design. You may want to consider subclassing, or defining a new class for your variables and passing a reference to it. The details will depend on your specific situation - it's hard to tell without seeing the code.
Upvotes: 2