Functions, Classes, and OO (Oh My!)
Overview
Teaching: 15 min
Exercises: 0 minQuestions
What is this ‘object oriented’ thing people talk about?
When should I write or use classes?
Should I feel bad about not writing classes?
Objectives
Understand object oriented programming
Be able to use classes
Not feel bad for not writing classes
Object Oriented (OO) programming
You may have heard that python is an object oriented programming language. In python, everything that you work with is an object, though it’s not always obviously so.
In programming languages, objects are a collection of data (attributes) with an associated set of functions (methods) that operate on these data. In effect, an object is something that can store some kind of internal state.
When you work with python, any time you call function such as message.upper()
you are calling the method upper()
which is part of the message
object.
Different types of objects can have methods with the same name, and they can behave in the the same or different ways.
If we want to create new object types then we create a Class
, which is a description of how the object works, and then create new objects of this type using variable = MyClass()
.
We can have multiple instances of the same class, each of which are individual objects.
Functions in python
Python also allows us to create and use functions that are not attached to objects.
These would be functions like int()
which would parse a string into an integer or math.sqrt()
which would return the square root of a numeric value.
We have already created some of our own functions earlier in this workshop.
Classes in python
The how/when/why of using classes in python is a long discussion. Any program that you can write that uses classes can be written without using classes (and vice-versa).
This means that you can get by without ever creating your own classes in python, however there are a few instances where a small class can be a quick and easy fix:
- A data class that holds information about an entity, but doesn’t define any functions.
- e.g. a
Galaxy
class that attributes like ‘redshift’, ‘ra’, ‘dec’, ‘name’ - These are super easy to make with the dataclass module / decorator
- e.g. a
- A config class that holds a bunch of configuration settings for your code.
- Passing a “config” object to a function is easier than setting 10+ arguments.
- A position class that is created with ra/dec coordinates but has functions that return the coords in different formats (though see astropy.SkyCoord)
Functional or OO programming?
Python allows us to work in an OO paradigm, but also in a functional or programmatic paradigm. (In fact we are able to use a hybrid as well). Note that PEP8 doesn’t make any recommendation about which of these paradigms should be used! (None of the PEP documents do to my knowledge).
Some people may feel that they are not “real programmers” because they don’t use objects. This is false for two reasons:
- Everything in python is an object, so you are already ‘using’ objects,
- Creating your own custom classes is not a requirement for writing good / effective code.
At the end of the day, the first and most important metric for your code is that “it works”. The way that you achieve this “work-y-ness” is up to you.
Ideally you will follow many of the best practices that we are covering in this workshop, but there will always be exceptions to the rule, reasons why you will deviate. The choice of programming paradigm, language, or operating system are all for you to choose. Some languages are more suited to particular tasks. Not choosing a language because you don’t have experience with it is a valid reason!
Key Points
You don’t have to create your own classes if you don’t want to
Classes can be useful but they are not essential