How to use Python classes effectively | by Ari Joury | Towards Data Science

“There should only be one — and preferably only one — obvious way to do it”, says the Zen of Python. Yet there are areas where even seasoned programmers debate what the right or wrong way to do things is.
One of these areas are Python classes. Borrowed from Object-Oriented Programming, they’re quite beautiful constructs which you can expand and modify as you code.
The big problem is that classes can make your code more complicated than necessary, and make it harder to read and maintain. So when should you use classes, and when should you use standard functions instead?
This story is a deeper dive into the matter. So if you’re in a hurry, you can skip the following two sections and scroll right down to the sections When to use classes and When classes are a bad idea.
Classes are objects that allow you to group data structures and procedures in one place. For example, imagine you’re writing a piece of code to organize the inventory of a clothes shop.
You could create a class that takes each item of clothing in the shop, and stores key quantities such as the type of clothing, and its color and size. We’ll add an option to add a price, too.
class Clothing(object):    def __init__(self, type, color, size, price=None):        self.type = type        self.color = color        self.size = size        self.price = price
Now, we can define various instances of the class and keep them organized:
bluejeans = Clothing("jeans", "blue", 12)redtshirt = Clothing("t-shirt", "red", 10, 10)
We would add these two lines without indent, after the definition of the class. This code will run, but it’s not doing very much. We can add a method to set the price directly underneath the __init__ function, within the class definition:
    def set_price(self, price):        """Set the price of an item of clothing."""        self.price = price        print(f"Setting the price of the {self.color} {self.type} to ${price}.")
We could also add some routines to tell us the price, or to promote an item by reducing the price:
    def get_price(self):        """Get the price of an item of clothing, if price is set."""        try:            print(f"The {self.color} {self.type} costs ${self.price}.")        except:            print(f"The price of the {self.color} {self.type} hasn't been set yet!")    def promote(self, percentage):        """Lower the price, if initial price is set."""        try:            self.price = self.price * (1-percentage/100)            print(f"The price of the {self.color} {self.type} has been reduced by {percentage} percent! It now only costs ${self.price:.0f}.")        except:            print(f"Oops. Set an initial price first!")
Now, we can add some calls of our methods after the lines where we’ve initialized the instances of the class:
print("blue jeans -------------------")bluejeans.promote(20)bluejeans.set_price(30)bluejeans.get_price()print("red t-shirt ------------------")redtshirt.get_price()redtshirt.promote(20)
If you run the script, the output will be the following:
blue jeans -------------------Oops. Set an initial price first!Setting the price of the blue jeans to $30.The blue jeans costs $30.red t-shirt ------------------The red t-shirt costs $10.The price of the red t-shirt has been reduced by 20 percent! It now only costs $8.
If you need to add more routines, you can just put them in the class definition.
The nicest part of all of this is that you can add and delete as many objects as you like. Deleting an attribute goes like so:
del redtshirt.price 
And if you want to delete an entire object, you do like so:
del redtshirt
All of this is neat, simple, and expandable. Try doing this implementation with standard functions, and you’ll probably have a lot more trouble dealing with it.
From a theoretical point of view, there are more reasons why Python classes are a beautiful concept in many situations.
If you’ve attended lectures in computer science, it’s pretty likely that you’ve stumbled across the principle of “separation of concerns”. It basically means that you split up your program into different sections that deal with different pieces of information.
Classes, by their nature, allow you to keep to that principle. In other words, when you set out writing a program and you’re thinking in terms of classes, you might be building a good architecture because you’re ensuring that each problem has its own place.
Thinking in classes not only helps you keep features separate, but also independent of one another. Not only does this keep things neat and tidy; it is also a lot easier for maintenance.
Say you found a bug in one class: you could fix that bug without worrying about the other classes because there is no connection between them. Likewise, you could add new features without fearing that you’ll get tangled up with other pieces of the software.
By using classes, you’re ensuring that methods are only used on one set of data. This adds to the security of the code because you’re less likely to use functions where they don’t belong.
Storing data structures and methods together is also called encapsulation. Since all of this is hidden from the end user, this allows you to modify data structures and methods without compromising the user experience.
For example, you might have to build a method that is quite complex. The advantage of encapsulation is that a user doesn’t need to understand any of that complexity because they can use it like a black box.
It is completely possible to build black-box-functions without using classes. With classes, however, this type of functioning is practically ensured.
With classes, you only have to define a data structure once. When you define an instance of a class, that instance automatically inherits the given structure.
In addition, inheritance makes it quite easy to delete or modify pieces of an instance or the whole class. This makes the whole construct more flexible.
towardsdatascience.com
With so many advantages, it might be tempting to use a class for everything and anything. In practice, however, there are situations where using classes makes perfect sense, and others where it doesn’t.
As a rule of thumb, when you have a set of data with a specific structure and you want to perform specific methods on it, use a class. That is only valid, however, if you use multiple data structures in your code.
If your whole code won’t ever deal with more than one structure. If you only have one data structure, it really depends on the problem at hand. You can get a rough idea by sketching out your program with or without a class; usually you’ll see pretty soon which solution is simpler.
Another rule of thumb is this: If you’re tempted to use global variables to access data, it might be easier to define a class and build a method to access each piece of data.
A heap, unlike a stack, is a way of storing data in a more flexible way because it has unlimited memory size and allows you to resize variables. On the other hand, accessing variables is slower with a heap and you must manage the memory yourself.
If a heap suits your purposes better, you don’t need to define a class. Python’s inbuilt heapq, or heap queue algorithm, does the job for you.
You might be tempted to use a class because you’re constantly calling a function with the same arguments. In most cases, it’s a better idea to use functools.partial() instead.
It’s quite simple to implement. Say you have a function that multiplies two values, but you keep using it to double values. To avoid duplicate code, you could write this:
from functools import partialdef multiply(x,y):    return x * ydoubling = partial(multiply,2)print(doubling(4))
Way easier than defining a new class!
Some programmers get obsessed with classes because they’re so flexible and expandable. That’s why, even at reputable companies and seasoned developers, you might encounter code like this:
class newclass:    """defining a new class to do something awesome"""    pass
The idea behind it is that, as the code grows, this class might be needed for whichever new data structure and the methods that go with it. But this is no good habit!
Guess what these three lines of code do? Exactly nothing. And those lines are not exactly difficult to code. If you think you’ll need another class later on, and you really think that you could forget about that in the future, you could always leave a comment like this:
# initiate a new class here if needed for purpose XY
Although you’ll want to make your code expandable and idiot-proof, initializing a class that does nothing is a usually bad idea.
towardsdatascience.com
Classes are without doubt a powerful concept. Used correctly, they can make your code tidier, more readable and maintainable.
But they get overused a lot. And when used wrongly, they can pollute your code until you understand nothing.
Sometimes, especially in simpler programs, you could use a class or a bunch of generic functions, and the code would be very similar in its length and complexity. As programs get more complex, the differences get more prominent.
In this sense, the Zen of Python has upheld its verdict: most of the time, there is indeed only one good way of doing things, whether that is with classes or without. It is, however, not always completely obvious. The difficult part is recognizing which way is the good one.
If you have other questions regarding Python or other programming languages, let me know in the comments! Thanks to Lucas Soares for asking for tips on when to use classes and when not.