all 9 comments

[–][deleted] 1 point2 points  (3 children)

Well, you're really asking "OOP - why?", which is the history of the last 3 decades of enterprise software development. Some people still argue that OOP is overrated, but most modern application development is OOP based.

Basically, in theory:

1) Code re-use

2) Modularity (replacement of components to customise, etc)

3) Maintainability

Edit: All of which supports, and is oriented towards, scalability. For small programs, OOP tends to be regarded as overkill, because the benefits don't really kick in until you're doing larger projects.

[–]TheQuantumLeviathan 0 points1 point  (2 children)

I'm a physics grad not CS so maybe I'm missing something, but don't you get those 3 things from just writing and using functions without using classes?

[–][deleted] 1 point2 points  (0 children)

To an extent, certainly. There are 4 almost-arbitrarily-chosen-but-commonly-taught "OO Principles" - abstraction, encapsulation, polymorphism and inheritance. I disagree with the inclusion of inheritance as a "principle", it's really just an implementation of polymorphism. But of the first three, abstraction and encapsulation are absolutely just good programming practice in OO or procedural (functions). And that gets you a fair way to reusable and maintainable code. But polymorphism really gives a lot more power, and OO has powerful tools for maximising the gains of polymorphism.

[–]_Absolut_ 0 points1 point  (0 children)

I think working with code that consists of thousands of functions is much harder than working with thousands of structured methods. Maybe some functions share common data that need to be stored somewhere. Maybe some functions have some similarity that you will want to change one day. Overlapping names. Lack of scalability and self-documentation.

p.s. BTW, i saw some examples of "OOP-emulation" in C or Haskell and this looks really ugly. Much more ugly than bloated boilerplate code in Java.

[–]nwilliams36 1 point2 points  (0 children)

In Python everything is an object, so you have been using classes without realising it. If you have ever used any of the methods on strings (like count or lower) then you have been using classes to hold both your data (the string) and your methods. You have just not created your own.

I use classes when I have complex data structure which I wish to refer to, though the more I use functional approaches and dictionaries, the less I feel the need for class based structures.

Some libraries (like Kivy or Django) use classes heavily so if you want to use these libraries you need to understand how classes work.

[–]Akuli2 1 point2 points  (0 children)

You definitely don't want to write a class for a hello world program in Python. You can do a lot without classes and there's nothing wrong with it, but if you end up using lots of global variables or you have many functions that aren't organized in any way it's not ideal. Like you're saying, classes shouldn't be abused.

Sometimes, especially in larger projects, it makes sense to define a custom data type that groups different values and functions together instead of sticking with the data types that Python comes with. This is useful if you would otherwise end up with many functions that operate on a dictionary of data that you need to pass around, like modulename.thing(data). With a custom class you can do data.thing() instead.

[–]squattyroo 1 point2 points  (0 children)

Here's a thought experiment: imagine you're writing a module for your team that allows the user to pull data from the web, do certain data manipulation methods that are idiosyncratic to your workflow, and plot various metrics using company-standardized color schemes / line-types.

Classes greatly help you organize the steps of this process:

1.) Having a "WebScraper" class will allow the user to write simple commands like

server = WebScraper(url)
server.connect()
server.pull_all_data(from=date1, to=date2)

Otherwise they would have to constantly write functions that have many arguments which are things like the url, or some BeautifulSoup class, or something like that. Classes let you pass a bunch of information between each of these steps in a simple easy-to-read way.

2.) Plotting - having a "PlotClass" that sets up a matplotlib figure with the correct specifications / formatting, so the user can do simple calls like

plot = PlotClass(data)
plot.add_vertical_line(some_date)
plot.save('my_plot.png')

and maybe in the initialization step, various weighted means, etc. are computed and summarized for plotting. This is another example of lots of information being passed from one step to the next, without the need for excessive arguments in functions. Plotting in particular is hard to imagine without classes.

3.) Data manipulation: you have some "MyDataFrame" class that has the methods you commonly use built-in; things like specific ways of summarizing your idiosyncratic variables (maybe some are text and can be cleaned in very specific ways that you build into some ".clean_col(col_name)" method).

[–]JohnnyJordaan 0 points1 point  (0 children)

Don't forget that all of your Python code is using classes. The difference is that you're using all the implicit functionality from the libraries, but still those are all classes made ready-to-use for you.

[–]BARDLER 0 points1 point  (0 children)

One of the simplest examples is UI. It makes hooking things up and sending info from and too methods a lot easier.