Bottom Page

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
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#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).
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Save all values to pandas of multiple classes jenniferruurs 0 80 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  Help | Classes to use in real world soothsayerpg 9 453 Jul-08-2019, 07:30 PM
Last Post: nilamo
  Problems with inheritance with classes internetguy 3 477 Jul-04-2019, 11:59 AM
Last Post: metulburr
  Unexpected Output using classes and inheritance langley 2 207 Jul-04-2019, 09:33 AM
Last Post: langley
  Including classes from folder issue graham23s 1 307 Apr-03-2019, 07:33 AM
Last Post: Gribouillis
  Help me understand this... (Classes and self). Ceegen 15 1,014 Mar-31-2019, 10:41 PM
Last Post: Yoriz
  sum a dictionary of classes tobbs 2 337 Mar-16-2019, 02:38 PM
Last Post: tobbs
  Data Hygiene, classes and magic reappearance of data praevalens 2 217 Mar-02-2019, 04:05 PM
Last Post: praevalens
  Addition of 2 classes Naito 1 245 Feb-15-2019, 10:51 AM
Last Post: DeaD_EyE
  Help with classes lucaswinnerton 6 345 Feb-07-2019, 01:23 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)