1

I am working on a small project which I intend to spread out over separate python files for isolation of separate modules. Some of these modules just need to have static information that needs to be accessible across the program, and some have utility functions (for example, a geometry library for drawing objects).

Would it be pythonic if I just made a separate .py and added constants and methods without having to define a class? It seems far simpler than having to create a class.

rapidDev
  • 109
  • 2
  • 13
Muhammad Farhan
  • 359
  • 2
  • 4
  • 12
  • 3
    Why wouldn't it be? Python isn't Java, it doesn't insist on object oriented purity (and then ignore it and include special primitives no one can duplicate). If a class isn't called for, don't use classes. – ShadowRanger Oct 04 '18 at 02:45

2 Answers2

1

Only create a class if you need to - Object Oriented Programming is not a solution to everything. However, if you find that you are overwhelmed with a monolithic script, it might make sense to break it up into different logical pieces. For example, a configuration file could hold all of your variables and 'static information'. Easily access it with an import and a file.open loop. If you find that there are too many methods and they could be easily logically sectioned, do whatever feels right. At the end of the day, you can get a working program both ways - it's about comfort and time. Balance the two and see where you get. Don't worry about it until you feel like it's an issue. Good luck with your project!

0

I think if you have several constants that need to be shared across many files, then defining them in their own file is a nice solution. This post corroborates that idea and shows you how to handle the imports. Utility files that house only functions are quite common as well. This post discusses both the class and the function-only options.

adamconkey
  • 4,104
  • 5
  • 32
  • 61