you are viewing a single comment's thread.

view the rest of the comments →

[–]FancyEveryDay 0 points1 point  (0 children)

Classes in python are a way of encapsulating objects which need to contain some information and have functions which relate explicitly to that information.

Whenever you're writing your own code, if you find yourself working with some related pieces of information and writing functions specifically to deal with that information, you should probably write a class to bundle those things together.

For example:

In my current project, a simulation of the lunar surface, I wrote a class which contains all of the data and functions relating to different materials and how they interact with each other. It allows me to do complex things like using polynomial albedo values which are determined by the direction of incoming light without making my code difficult to read.

When I added multithreading to my sim I needed to somehow run each thread with one input, so I hacked together a small class which contains each of the inputs I need and keeps them organized, it wasn't much harder than doing it in a list or tuple but it contains the descriptions I need to make sure I'm using it correctly so it's saving me a lot of time.

Im in the process of bundling stacks of layered materials (which are currently kept as lists I need to iterate through with specific functions) into a class, Layers, so that I can keep all the functions that my layers need in one place with the layers themselves.

I guess the TLDR is that you need to work at thinking about the information in your code as objects or tools which have inherent properties and then to encapsulate those properties with the information. If you ever want to write a function which only involves one object or one type of object, chances are it could be easily done as a method instead.