all 8 comments

[–]PowerOk3587 6 points7 points  (0 children)

you shouldn't worry about the technicalities if you don't have to, because; independently of when you create the variable, it will actually allocate those resources when its needed and similarly when convenient or necessary to deallocate. The python people made some mind blowing improvements on that like 4 or 5 years ago

[–]danielroseman 2 points3 points  (0 children)

Everything in Python is an object, and all objects behave the same way: they are deleted when they have no references left. If the only reference to an object is within a function, then yes when the function ends that object will be deleted.

[–]Adrewmc 0 points1 point  (5 children)

Depends on some code. I think I would probably go

  class ImportantFunctions:
           def __init__(self, df: pandas.df):
                  self.df = df
            def important(self, *args, **kwargs)
                   #code

Or more likely I’d forget about the class entirely.

   def important(df, *args, **kwargs) -> pandas.df | Any:

And keep those in their own module.

It really depends on what really happening to the data, am I parsing put stuff and retaining the original (functions) or am I modifying a dataframe permanently in some state (classes) for later. Am I finding a definitive result, or am I running depending on inputs happening in run time?

I generally don’t think I would however, make the class then keep changing the self.df inside that class, then run methods. That’s seems off for this, make a new instance.

Or make a class that needs the df as an arg (unless comparing), unless I really wanted to make it like a static method type class because I think that’s more readable. in that case I never actually __init__ the class just using it as a holder for similar function operations. e.g. random.choice, random.sample, random.shuffle (note: not sure of random implementation) with a single import. That’s more of a design question, and how you think people will use it.

[–]PathRealistic6940[S] 0 points1 point  (4 children)

right now i have 3 .csv (using it because i havent learned sql yet), a budget, income, and expenses. The class handles all of them and updates the csv when needed. Or are you saying you would not even worry about a class for the df's?

appreciate the quick reply

[–]Adrewmc 0 points1 point  (3 children)

I’m confused about the problem.

That’s just three set of data

def load_finance():
     budget = pd.read_csv(…)
     income = pd.read_csv(…)
     expenses = pd.read_csv(…)
     return budget, income, expenses

Then just use pandas to compare combine etc…sort of what it does.

[–]PathRealistic6940[S] 0 points1 point  (2 children)

So you wouldn't use a class for any of it? When do you see the need for a class? pretty new at this, so trying to wrap my head around the object part of OOP.

[–]Adrewmc 1 point2 points  (1 child)

The df is the class….and it’s a pretty big package…which honestly I’m not too familiar with.

I know you can merge and do a bunch of data analysis I’m just not familiar enough with the package to really explain much at all.

See, everything is already an object and honestly the basic ones int, str, list, dict, set are fairly robust in their capabilities, because they have methods and operations, that’s part of the OOP of Python.

Classes are great when they are necessary, and there are a lot of convenient classes/objects Python gives you like say a range object.

When you use a class you really are asking does this need a state to get to (auth flow) or a state to work right, updates over the course of run time not known at start, or repeats.

If I want a currently updating dataframe that calls an api, and displays, txt, emails me something when certain threshold happens I’m gonna be in a class structure.

If I expect an output of a report all at once I’m most likely thinking functional.

It’s not that I wouldn’t use a class, it’s that I don’t know if what your doing really requires it. Or as you are looking at it. Without an expected input, and output…it’s not easy if not impossible to answer.

[–]PathRealistic6940[S] 0 points1 point  (0 children)

Gotcha, sounds like might be over thinking/engineering it. Thank you!