0

As the title suggests, I'm interested in the best (perhaps the most Pythonic way) to structure a program which uses many global variables.

First of all, by "many", I mean some 30 variables (which may be dictionaries, floats or strings) which every module of my program needs to access. Now, there seem to be two ways to do this:

  • define the "global" variables in seperate modules
  • use an object oriented approach

The advantage of using an object oriented approach is that I can have many instances of some main class initialized, and perhaps compare different values (results of some analysis, for example) later on.

I already have a program written, but basically it breaks down to one class with some 30 or so attributes. Although it works fine, I'm aware this is a pretty messy way to do this.

So, basically, is I use OOP approach, I would perhaps need to break my main class down to a few subclasses, every one of which stores specific logically related variables.

Any suggestions are welcome.

P.S. Just to be concrete about what I'm trying to do: I have a FEM-solver which needs to store structure info, element and node data, analysis result data, etc. So, I'm dealing with a lot of data types most of which are connected in some way.

  • 4
    I think the most pythonic way is to not use globals :-) – Tim Sep 26 '15 at 16:45
  • 1
    i disagree, most frameworks implement "settings.py" like django for example, and it's not a bad thing. By I would use it only for constants, not variable data, NEVER. – DevLounge Sep 26 '15 at 16:49
  • @Apero. So, in fact, you *do* agree (given that you never modify such globals). – ekhumoro Sep 26 '15 at 16:54
  • indeed then, we do agree actually. Globals are to be avoided like pest IMO – DevLounge Sep 26 '15 at 16:57
  • I would also like to stress out that basically all of my "globals" are being modified. The only ones which are constants are some settings-type variables. – John Kimble Sep 26 '15 at 17:02
  • "So, I'm dealing with a lot of data types most of which are connected in some way." is symptomatic of poor class design. – msw Sep 26 '15 at 17:06

1 Answers1

2

Unfortunately, as was hinted at in the comments, there is no "Pythonic" way to do this. Having a large number of global constants is just fine - many programs and libraries do this. But in the comments, you've specified that all of your globals are being modified.

You need to take your program's architecture back to the drawing board. Rethink the relationships between your program's entities (functions, classes, modules, etc). There has to be a better way to organize it.

And by the way, it also sounds like you're getting close to using the God Object Antipattern. Use some of the advice in this SO question to refactor your massive class that has it's fingers all over your program.

Community
  • 1
  • 1
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
  • Some additional details: my program is already split into modules, which are divided by specific tasks pretty logically. These modules contain mostly functions. The big class I use (and the only one in the program) contains attributes only, so it's operated on by most of the modules. The only "problem" now is where to put these non-constant attributes. Indeed, distributing these attributes (i.e. potential variables) among a few newly created modules does seem logical to me. When I think about the program's architecture again, I don't see another intuitive way than this one. – John Kimble Sep 28 '15 at 15:51
  • To refer to my comment above, there actually might be a better way to do this: in a later step, my program creates a database which can be viewed later on. Since the program takes the input data from an external application in the form os a .csv file, one could create a database in the beginning with all the relevant data for calculation. I'm only not sure if this will slow down performance? I have read about in-memory databases which are possible with Sqlite. These should be sufficiently fast, I assume? This would be a way to completely avoid the use of non-constant "globals". – John Kimble Sep 28 '15 at 19:36
  • @JohnKimble I haven't used the in-memory databases before, so I can't comment on how using it will impact your program's performance, but if you replace your global references with database references, that's only a marginal improvement. You're still making your program difficult to debug, by having a global data structure that is modified from many places. Since your class seems to just be a data container, if you can't refactor to reduce the number of data dependencies, *just don't make it global*. Create the object and pass it properly via function arguments instead. – skrrgwasme Sep 28 '15 at 21:42
  • So basically, you're saying that my data structures should only be created/handled where they need to, right? Also, I'd like to stress out that my code wasn't actually hard to maintain and debug, only that this huge class looked pretty silly. – John Kimble Sep 29 '15 at 12:02