Follow

I think I went a little overboard with classes when they finally clicked.

I'm looking through some of the scripts I made at work to automate my reports, and all but one could have the class removed in favor of just one or two functions... I think I just wanted to use classes because it made me feel cool or something 😅 or like I knew more than what I was doing.

@pinguino I haven't been doing Python very long, and I understand classes and how they work, but I have yet to find a reason to use one in my actual work projects. Maybe it's because most of my stuff is just baseline process automation. It's not terribly exciting, and I'm not writing anything that really blows anybody's hair back.

@mike Most everything I've done with it so far has been automation and data analysis with the pandas lib, and the only time where I've seen it be really useful has been when I used a class to initialize the data (in my case mostly pulling in data from a spreadsheet and putting it into dataframes), then using methods to mess with it.

All it really does is make it more convenient. Rather than having to return a dataframe, I can just keep track of it as an attribute and share it amongst functions.

@mike @pinguino Real world example from this week: I have 6 subclasses that inherit from a Fetch parent class. The parent class does some initialization, calls a make_request method, and does cleanup.

The subclasses implement the make_request function, which is very different for each subclass.

All the common stuff is in the parent class. We adhear the to DRY priciple and save dozens of repeated lines of code.

Often we only need to change one place in the parent class, not each subclass.

@pinguino I think this is very normal as you progress in a language. When you learn constructs and patterns you want to try them out and learn by building things with them.

As you progress you discover that sometimes OOP introduces unnecessary complexity and doesn't guarantee good design. You can usually create elegant solutions and DRY code without touching classes at all. That said, OOP in Python is still very useful for a range of problems, so definitely worth having in your skillset 😀

Sign in to participate in the conversation
Fosstodon

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.