Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using classes? Can I just use classes to structure code?
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
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.
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.
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.
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).
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.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  classes mp3909 11 426 Jun-10-2020, 08:56 PM
Last Post: snippsat
  how can I change xcor and ycor of the turtle if i am using classes MohammedSohail 1 155 May-22-2020, 06:28 PM
Last Post: deanhystad
  Database structure (no code?) BlackmoreSteve620721 1 171 Apr-13-2020, 10:39 PM
Last Post: Larz60+
  Do lists of classes take up more space in memory? DreamingInsanity 1 179 Apr-10-2020, 01:50 PM
Last Post: deanhystad
  Creating function inside classes using type wolfmansbrother 3 337 Mar-20-2020, 01:21 AM
Last Post: wolfmansbrother
  Relating 2 Classes wew044 2 253 Feb-15-2020, 04:19 AM
Last Post: wew044
  ppadb classes alejandroot 2 361 Feb-06-2020, 04:44 PM
Last Post: alejandroot
  Some help with Classes and the random module please? :) AlluminumFoil 2 237 Jan-08-2020, 11:03 PM
Last Post: AlluminumFoil
  Classes GFreenD 1 215 Dec-17-2019, 10:55 AM
Last Post: buran
  Getting attributes from a list containing classes midarq 2 308 Dec-03-2019, 12:42 AM
Last Post: midarq

Forum Jump:

Users browsing this thread: 1 Guest(s)