Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python OOP
#4
The syntax for doing this in python is very easy since you just create a python file and use it as a module in your main file. How you decide to build a file structure is the tricky part and it depends on how you want to think about your code. It's also a very personal decision as some coders love the Model-View-Controller paradigm and break things down into data, GUI and interfaces. Others would rather keep adding folders as they add new features so they might have folders like GUI, Database, Analysis, Reports, etc. This also depends on the App as well.

I suggest you just start simply with a sub-folder called "Classes" to get going. You can always add sub-folders later with little effort. Or, you could just name your classes with long strings that define their function. Remember, python doesn't care where the modules it imports are. That's all to help you and whoever reads your code so unless you have a paying job that defines how it must be done you can decide how much effort you want to put into it.

Once you have the file structure figured out you just write your classes in it and import them in the main code file. I'm doing a program that analyzes data from a batch of DUTs (Device Under Test) so I have a DUT_Data class in a DUT_DataClasses module. (It's very creative naming isn't it! Liar ) Right now it's not even in a sub-folder but I will expand the folder structure as things get more complicated.

In my main program I import the class with:
from DUT_DataClasses import DUT_Data as DD  # I use the 'as DD' to keep it short.
Then I just instantiate the Dut_Data objects (now called DD) and use the functionality inside.
Reply


Messages In This Thread
Python OOP - by Manolo029 - Dec-27-2020, 01:15 PM
RE: Python OOP - by ndc85430 - Dec-27-2020, 01:18 PM
RE: Python OOP - by Manolo029 - Dec-27-2020, 01:35 PM
RE: Python OOP - by Marbelous - Dec-28-2020, 04:14 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020