tags:

views:

95

answers:

6

Hi,

I'm a mechanical engineering student, and I'm building a physical simulation using PyODE.

instead of running everything from one file, I wanted to organize stuff in modules so I had:

  • main.py
  • callback.py
  • helper.py

I ran into problems when I realized that helper.py needed to reference variables from main, but main was the one importing helper!

so my solution was to create a 4th file, which houses variables and imports only external modules (such as time and random).

so I now have:

  • main.py
  • callback.py
  • helper.py
  • parameters.py

and all scripts have: import parameters and use: parameters.foo or parameters.bar.

Is this an acceptable practice or is this a sure fire way to make python programmers puke? :)

Please let me know if this makes sense, or if there is a more sensible way of doing it!

Thanks, -Leav

+2  A: 

Uhm, i think it does not make sence if this happens: "realized that helper.py needed to reference variables from main", your helper functions should be independent from your "main code", otherwise i think its ugly and more like a design failure.

InsertNickHere
A: 

I'm not too sure about if that's good practice but if you use classes, I don't see why there should be a problem. Or am I missing something?

If you want to be able to just run each script independently too, and that's what is keeping you from going object oriented then you could do something like the following at the end of your script.

if __name__ == '__main__':
    # Code that you want to run when the script is executed.
    # This block will not be executed if the script is imported.

Read more about classes in Python here.

Ashish
I'm not sure I understand the distinction between classes and modules, could you point me in the right direction?How would I use classes to in this context?
Leav
I'll go an abstraction higher: first grok the concept of *Object Oriented* programming.
Santa
I think I basically understand the concept of OOP, but am lacking the experience in how to implement it. would you be kind enough to just give a quick example which pertains to the above simple case?
Leav
Yes, that can be done. Look at the file [here](http://bitbucket.org/ashishg/chunky/src/tip/Chunky). Under the last `if` block you'll see code on how to use classes. And the class can be accessed using `classInstance.variable`. I hope this helps. So in your `main` script you import your other scripts. Create your `objects` (which is called an `instance` of the `class`, code of which is in the other scripts) and access the `instance`'s variables. Hope this helps.
Ashish
Thanks Ashish!one last question: could I access the same instance from seperate modules, or is that still a bad idea?
Leav
Unless you share the state somehow (like files, etc.) I don't think you can. I would recommend a separate module which would import all interacting modules and *glues* them together.
Ashish
+3  A: 

Separate 'global' files for constants, configurations, and includes needed everywhere are fine. But when they contain actual mutable variables then they're not such a good idea. Consider having the files communicate with function return values and arguments instead. This promotes encapsulation and will keep your code from becoming a tangled mess.

Clear communication between files makes them easier to understand and makes what's going on more obvious. When you're using variables and nobody knows where they came from, things can get pretty annoying. :)

CrazyJugglerDrummer
+1  A: 

I try to design my code so that it looks much like a pyramid. That, I have found, leads to cleaner code.

Paul Nathan
Paul, do you mean design it so that no module is dependent on a module above it (or adjacent to it) in the pyramid?This makes sense and would solve my problem (if i could redesign my code that way).in your experience, can the majority of projects be designed this way?
Leav
@Leav: Yes, I work toward that. I know a *lot* of *my* projects can be designed towards that fashion. I won't go so far as to generalize that to the majority of projects.
Paul Nathan
Thanks, This is what I ended up doing and it seems to be working!
Leav
A: 

You should probably read up on Dependency Inversion.

pillmuncher
A: 

Seems like what you want is to organize various dependencies between components. You will be better off expressing these dependencies in an object-oriented manner. Rather than doing it by importing modules and global states, encode these states in objects and pass those around.

Read up on objects and classes and how to write them in Python; I'd probably start there.

Santa