Python Forum
Using classes? Can I just use classes to structure code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using classes? Can I just use classes to structure code?
#1
Hi,
I'm self-teaching myself Python using examples and the excellent documentation. However, I'm stuck trying to understand classes. Do I need them? Can I structure the code with them? Do they introduce too much complexity? Do they apply to my project?

My project is a program that uses a single random seed to generate a made-up "record label", with multiple "artists", each with multiple "albums", each with multiple "tracks", which are generated CSound music code files. It also names the artists, albums, tracks and members of each band, and it generates artwork and album covers. As a side feature it generates a "Rock Family Tree" of the
(If anyone was interested in learning more about this project, PM me. I'd be glad of collaborators!)

There are functions that generate lists of words, functions that generate lists of note pitches, note lengths etc. A function iterates through the list of artists, another iterates through the list of albums for that artist etc etc.

I'm using PyCharm on Linux, which is a great powerful free IDE, with smart folding features. I'm using functions, and modules to break the functions into a separate file. The main file is still quite big, and I would like to be able to structure it to be able to navigate to bits of it easily.

Can anyone advise me of the best way to structure this project? Does it warrant classes? How would you go about structuring the nested functions of this project? Is there any way to flatten what might be too much nesting?

Any advice greatly appreciated. I can post code later from home if necessary.

Best regards,
Matthew Petty
Dubai
Reply
#2
It sounds like your project can benefit by using classes. Most probably your code can be refactored and many of the functions will become methods and properties of the different classes.
Of course, obviously, it can be done without classes too.
Reply
#3
I just posted the following on another thread, but will repeat here:
Classes keep related functions organized into one box. The objects held in that box are functions, but since there are within the scope of the class, they now become 'methods' by name.

Everything within a class, is capable of knowing everything else, that's within the class, and easily interacting with the other components.

when you import a class, you don't only get the object that you are interested in, but all supporting methods as well. If you only intend to use a portion of that, you can use 'from' ... import ...

I like to compare classes vs functions to an automobile service garage.

A function is a tool. A class is a chest of tools, all organized by their use for a particular function. It's easier to get a brake tool in the brake tool chest, rather than searching the garage for that same tool.
Reply
#4
Thanks for the suggestions. I think I will use classes.

But, I don't understand how my code, variables, and functions would fit into classes. I have functions that generate lists of words, that generate tunes, that generate artwork. How do I divide them up into classes?

I guess I don't really understand the concept and application of classes.
Reply
#5
Think of a class as a collection.  Like a dictionary, that can have actions instead of just data.  For your example, you'd have probably an Artist, a Record/Album, and maybe a Track class, if you had some tracks that were on multiple albums (collections, or collaborations).
Reply
#6
Quote:Can I structure the code with them? Do they apply to my project?
They apply to every project. I would always use classes if the code requires it. I wouldnt go crazy like Java though. If you need it, you need it, if you dont, then you dont.
Quote:Do they introduce too much complexity?
They actually do quite the opposite. They simplify code. You can get away without them for sure. C doesnt have any classes, and python is written in C. But C is extremely low level.

Quote:But, I don't understand how my code, variables, and functions would fit into classes. I have functions that generate lists of words, that generate tunes, that generate artwork. How do I divide them up into classes?
Its a little harder to rewrite a program that was created without classes in mind, than it is to write a program from scratch using classes from the get-go. You basically have to rewrite the program. You should at least be able to have the knowledge of how to switch it to classes though.

An example of code VS code that has and does not have classes would be here. Its regarding pygame, but you dont need to know any of that in order to understand the concept of classes VS no classes. You can see how a bunch of related data and executions go into one class. The comments you can somewhat ignore as they pertain to pygame, or are just showing old code. I made this comparison awhile ago to show people that this youtuber's tutorials were bad. I dont remember how exactly long it took me to convert his code, but it wasnt a sit down thing i did in the time of a lunch break or something.

Both of these codes result are the same thing. But if you were looking at the two codes in comparison you can see where things are in the classes one. If i want to know what the players image was, i know to first find the Player class, then the image attribute. Whereas if i am looking in the first example, i am scanning all over trying to find it. There is organization in classes that doesnt exist without them. And this is with every single attribute, not just one. In fact i had to condense his player stuff into the player class, like keyboard handling. No one else is going to use the keyboard except the player so it might as well get organized into the Player class. Any more code pertaining to Player would go into the Player class to keep it organized.

The global function message_display stays a global function because it does not belong to Car or Player.

You can break it up even further to a class per module if its something like a Player class, but that might not be always the case. In this example there are a ton of classes in one module. Where there is only one class in this module and would never need more.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 468 Feb-05-2024, 06:51 PM
Last Post: rob101
  How do I classify colored images into 3 classes max22 0 655 Dec-04-2023, 10:33 PM
Last Post: max22
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 891 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Organizing several similar classes with overlapping variables 6hearts 7 1,331 May-07-2023, 02:00 PM
Last Post: 6hearts
  Q on classes ebolisa 4 831 Mar-06-2023, 04:17 PM
Last Post: deanhystad
  which design / pattern when building classes and subclasses Phaze90 2 1,086 Nov-19-2022, 08:42 AM
Last Post: Gribouillis
  Understanding Python classes PythonNewbee 3 1,150 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 995 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  super multiple parallel classes catlessness 2 1,294 Jun-07-2022, 02:35 PM
Last Post: deanhystad
  Adding Decimals to classes with OOP + rounding to significant digits (ATM demo) Drone4four 7 2,243 May-04-2022, 06:15 AM
Last Post: Drone4four

Forum Jump:

User Panel Messages

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