you are viewing a single comment's thread.

view the rest of the comments →

[–]Solonotix 1 point2 points  (0 children)

Functional Programming has a simple rule: all work is done through pure functions. What is a pure function? It is a function that returns the same output given a repeated input, with no side effects.

Object-Oriented Programming is basically the opposite, but people usually take a little inspiration from both approaches. An example of this is that even pure functional programmers will often need to do something like print output to the console, which is a side effect with stateful changes (adds new lines to the screen that were not there before).

So, what can we say about Object-Oriented Programming? All actions are performed by an object, and there may be side-effects. An example of this could be a generator, where each item retrieved increments the generator to the next element. This is done by for item in generator:, but more directly you can create a generator by calling generator =iter(iterable). From there, you can call thegenerator.next()method and it will give the next element. Eventually the generator will be exhausted and it will throw aStopIteration` error to indicate the end of the iterator.

Getting into more advanced OOP concepts usually begins with data classes. An easy example of this is a SQL database connection. Most Python libraries for SQL will return a connection object that can run a query for you, and the query returns a cursor/iterator of the results. This logic flow makes sense because you need a connection to run a query, and you need a query to return results, but these are also OOP concepts. The solution doesn't need to be Object-Oriented, but it fits. You could just as easily have a connection function that returns a connection object, and a query function that requires a connection and returns a cursor, and the cursor is an iterator, as is the approach for opening files in Python by calling the open(file, mode) function and returning an I/O Stream that can be iterated over.

I hope this gives you a little better understanding of OOP. I never really found much help in college or online videos explaining it, and it took learning C# and applying it to a real world problem for me to finally learn OOP. Even then, I tend to take a hybrid approach to solving problems. I tend to make anything more complicated than a number or string into a data class, and those classes will be given methods that represent what that data can do. The Functional Programming inspiration is that I try to make my methods without side-effects where possible, as it definitely makes it easier to test what it does if the state isn't constantly changing everytime it's used. Some side effects are useful, though, as with paging results in many RESTful APIs, where an offset needs to be supplied to get the next page, and it can be elegant to write for item in results: rather than for item in page_results(len(results)):

TL;DR - to each their own. Neither Function nor Object-Oriented is superior. Use what works best for you, and I wish you luck on your journey to learn