Continuing our journey together in the world of OOP in Python, today, we are going to go deeper in one of the most important building blocks of the class which is the method. Up till now, we only used one type of methods (instance methods) that defines with self and considered to be the most common type of methods. While instance methods are great, there are two other type of methods: class methods and static methods. So, in this lecture, we will try to find out the (why, how, when) to use them. We will go only with one example; the Book class.
When I first began programming in Python, I was doing messy code (most beginner data scientist are famous for their messy code). Then, I learned functional programming; the Python main point of using functions is to avoid repetition as mention in PEP 8 performance tips (DRY) which is: don’t repeat yourself. While the true idea behind function programming is to use pure functions like map, filter, or reduce to transform your data without side effects—this means that the function only effects its input not other parts of the code.
Throughout our journey so far, we can build complex, functional systems. Yet, as our projects grow in scale and complexity, a subtle but persistent challenge emerges: the problem of trust.
Up till now, we worked with variables, strings, numbers, sequences, and data structures. But hello?! We are in the age of big data! Is that everything? Well, this is not every thing; you can also work with external resources (e.g. files on disk, database connections, network sockets, and GPU sessions). So, what makes managing these external resources important? The main reason is because they are limited like non-renewable energy sources. Also, if you forget to release them, your program can slow down or crash.
As I promised, we will for sure take about the thinking process not only the syntax choices and here I’m to live up to this promise. First of all, many python coder consider functional programming as the number two programming paradigm that comes object-oriented programming. By the way, “paradigm” represents a way of thinking about how problems are framed and solutions are expressed.
We’ve just saw the power of sequences and how the simple choice of the sequence type affects the performance significantly. Here, I’ll also take you in the same journey but with more complicated data structures that are built above sequences. And, the idea behind all of that is to give you the base knowledge to take a solid choices on the data type / data structure you will use. So, as always, we are not going to only see the how of each data structure work, but also we will go deep and discover why and when to use each.
Unlike “CS introduction with Python” courses, the goal here, from the beginning, is to use Python effectively so we need to learn as much Python features as we could. However, if you came from another programming language, you will probably search on applying that language’s features in Python. In that case, you will miss what Python offers. For example, if you’ve never seen it tuple unpacking before, you will probably not search for them, and you may end up not using this feature just because it is specific to Python (Ramalho, 2022). So, you need to keep your eyes open.
As they say “What is more important than doing the work is organising it”. So, what we aim to do today is to reach some level of simple, efficient, readable, and robust code. It is not another list of rules to memorise, it is how you share your thinking process with other coders. Also, a big part of your job as a coder is to share your ideas, contribute, or at least do your tasks within your team/company.