Skip to main content

Getting started with classes in Python

james.derrick@ansys.com | 10.17.2024

When one is just starting out on one's journey to learn a new language like Python or C# or other "object-oriented" languages one tends to learn a lot about the basic building blocks of all code; stuff that is common to (almost) all languages. Variables, functions, loops, collections, types, and so on and so forth. However, also when just starting out, especially with scripting languages like Python, there are levels of learning and the very first level of basic training often has to leave out some concepts that are too complex, or tricky to understand. Python training in particular often doesn't reach a concept that many see as one of its most important aspects to know about: "Classes".

For example, my own "Introduction to Python" course stops just short of classes and I often get asked "why?". The answer is very simple, and is that there just isn't enough time to cover them in the detail they warrant, as the training is already too long, if anything.

If you are having trouble understanding what this means, think of classes as being analogous to paragraphs. When learning to read and write, you most likely focused on the absolute basics first, like how to recognise individual letters, and how to replicate them yourself. Then you went onto sentence structure, spelling, and grammar (though not in that order). Once you had that worked out you'd have been considered mostly literate. At this stage you can read most text, although you're probably not well-versed in the finer points and your vocabulary is probably quite small. At this stage your skill and knowledge-level is analogous to someone that has taken a basic programming course, like my "Intro to Python". The next step in your reading journey would be to learn about paragraphs: a simple way of organising blocks of text to instil meaning and also just to make reading easier. Paragraphs are fundamentally just a structure used to break up text, making it easier to read, as well as convey information. Classes are the equivalent next step. Functions are like sentences and classes are like paragraphs. They are another way to group text, and code, that are so common in the worlds of literature and programming that learning them is an inevitability for anyone who wishes to write or program.

In this series of articles I will cover exactly what classes are, what they look like, and how to make them, as well as a selection of tips and tricks surrounding them. By the end of this series you will have the information needed to build your own classes in Python, with their own methods, properties, and dunder methods.

  1. Part 1: Anatomy of a Class
  2. Part 2: dataclasses
  3. Part 3: Methods and properties
  4. Part 4: Hashing & Mutability
  5. Part 5: classmethod and staticmethod
  6. Part 6: Public, Private and Super-Private Naming
  7. Part 7: getters and setters
  8. Part 8: dunder methods
  9. Part 9: __repr__ vs __str__
  10. Part 10: Inheritance