Coming from 4 years of procedural / functional programming, I can't wrap my head around OOP.
Hello, r/learnpython,
I am a mid level Data Scientist / Software Engineer and I have worked the last 4 years of developing statistical libraries, mainly in R and C. Due to the market and the direction my company is taking, I am being pushed to start developing in Python, but I have never (???) written proper OOP Python libraries in production.
And yes, I know R has S3, S4, R6, S7, etc, I just don't like to use them as the ecossystem is not concise in OOP matters.
I have written lots of Python in production, but it was either backend, or just more procedural type pipelines.
Let's say I want to create a library (kind of like an ORM?) where I have some queries I make all the time and want to be able to pass parameters to those queries and retrieve the data. In R, for something like this, I would design the API like:
```{r}
#' u/export
get_full_data_list <- function(connection, database, country, ...) {
# NOTE: These will also be public with `@export` and `@inheritParams` from
# main
df1 <- get_df1(connection, database, country, ...)
df2 <- get_df2(connection, database, country, ...)
df3 <- get_df3(connection, database, country, ...)
list(
df1,
df2,
df3
)
}
```
This would create a public function exported to the user he can just call. I would also `@export` all of the getters for the df1, 2 and 3, so users could call them on their own.
How would I do this in Python with proper OOP style architecture? I was thinking of the UX to be:
```python
from pkg import MyConnection
conn = MyConnection(
connection = "databricks",
database = "gold_layer",
country = "Spain"
)
# `...` being aditional params needed for the query specifically
df1 = conn.get_df1(...)
```
But then, I would have to do something like:
```python
class MyConnection:
def __init__(self, conn, database, country):
self.conn = conn
self.database = database
self.country = country
def get_df1(self, *args, **kwargs):
# This would replace the args in f strings
query = get_df1_query(self, *args, **kwargs)
return execute_query1(self, query)
def _get_df1_query(self, *args, **kwargs):
# The f string stuff
pass
```
This seems like everything is going to be on the same file, and I feel like I am missing some other abstraction layer and other class for this to be ok-ish to ship and easy to maintain down the line.
Thank you so much, I am banging my head on the wall lol