1

In a nutshell: Is it possible and in good practice to have a list in one module be accessed and modified from several others or it better be restructured to be used in only the one in which they were created?

I am writing a game and as it got above 1500 lines, i thought it would be high time to restructure it into modules for easier maintaining. In the original program there were several lists, that stored gameobjects like tiles and such. These lists were accessed for iterating over, and also to be appended to from several classes.

I thought of putting all lists/dicts that need accessing from several others into their own storage module, and importing the appropriate containers into the modules they are needed in, but it didnt seem like it worked.

Can this be done properly?

Kalina Attila
  • 33
  • 1
  • 6

2 Answers2

1

There are good reasons to have an object that stores global state be accessible from several modules. The scenario you describe sounds like an appropriate use case for this.

The proper way to do this in python to maintain some good semantics is to create a Singleton class with all these lists (or separate Singleton classes if the lists represent information that is different conceptually to each other) and then have the other modules modify these Singleton objects (ie, call methods on them) to append things to the lists. Singletons are classes that can only be created once. If you try to create them again, the first object is returned and no new object is created, essentially. So you could have Singleton called 'GameState'.

Python doesn't support creating Singleton objects natively, but there are ways to work around this, look around stackoverflow for how to create a singleton in python

Community
  • 1
  • 1
grasshopper
  • 3,988
  • 3
  • 23
  • 29
  • 2
    the concept of singleton-object is identical to modules. You can import a module everywhere, but it is only created once. – Daniel Nov 16 '14 at 09:34
  • You know what, you're right about this. Having objects as opposed to modules still has the advantage of being able to pass their instances around though, if one wants to decouple the fact that they are singletons from some methods that call methods on them. – grasshopper Nov 16 '14 at 09:44
  • 1
    @grasshopper modules are objects too: you can pass an instance of a module around in exactly the same way. – Duncan Nov 16 '14 at 10:38
  • @Daniel: Singleton instances and modules aren't completely equivalent. See the question [_When to use a Singleton in python?_](http://stackoverflow.com/questions/14600075/when-to-use-a-singleton-in-python) (and [my answer](http://stackoverflow.com/a/14602891/355230) in particular). – martineau Nov 16 '14 at 11:01
1

The way, to write a storage module seems correct. But without knowing the details, I can only suggest some good practices, with might be not suitable for your case. Static objects, like tiles should be loaded/created only at one place. The access to these objects should happen over a definite interface implemented in one module.

Daniel
  • 42,087
  • 4
  • 55
  • 81
  • Thanks, based on these answered i seem to have found a way. module 'A' being the storage and the interface for manipulating. modules 'B','C'..and more importing A and calling the interface. Sorry couldnt work the code into the comment. – Kalina Attila Nov 16 '14 at 10:32