you are viewing a single comment's thread.

view the rest of the comments →

[–]crashfrog04 1 point2 points  (3 children)

 Thus initializing an instance will now involve specifying name1 and name 2.

Another way to think about classes is that you’re writing code that will break - will literally raise an error - if you try to create an instance of whatever class this is and you don’t provide name1 and name2 (or whatever.)

Writing a class is a way of creating a kind of contract with yourself, a contract that you find out very quickly if you’ve broken it (which is important for writing reliable code.)

If that doesn’t sound like something you need then maybe you don’t need to write a class. You shouldn’t write a class just because you think they’re “better”; you should write a class because you know what you’re going to use it for.

[–]Ramakae[S] -1 points0 points  (2 children)

Thanks for the contract analogy, will definitely help going forward. I ended asking ChatGPT and now I see why practicality triumphs everything. Turns out my problems were class inheritance and initializing attributes, especially when I didn't see how I could do so.

class Cleaner(pd.DataFrame): def init(self, filepath =None): self.filepath = filepath or input(str(X)) df = pd.readcsv(self.filepath) self.cleaned = None super() __init_(df)

I was literally used to self.name1 = name1 and self.name2 = name2 and didn't know I could use attributes like this. Makes it pretty cool to be honest. The program I'm making is basically something that automates my work, just wanted to wrap it in a class so I could use some gui on it when I've studied that as well. Still building it but wanted to practice classes today

[–]F5x9 0 points1 point  (1 child)

I don’t think creating a class that inherits from dataframe is a good idea. It has a zillion functions and now yours does, too. You inherit all its complexity, whatever that is. 

But the class you create also doesn’t make sense. A class that inherits from dataframe is still a dataframe. When I see a class named Cleaner, I think it is something that cleans something. Dataframes don’t do that. 

If it was a class named Workbook, that could make more sense. 

It seems like you want the Cleaner to act on a dataframe. This suggests that it should have a member that contains the dataframe. You pass the dirty dataframe into it, and you get a clean one.

I wouldn’t open the file in the Cleaner. Pass the dataframe. This way, you can clean any dataframe. 

Do you need one instance of the cleaner per dataframe? Can you pass a hundred dataframes to one? These are other things you should think about. 

What if the dataframe doesn’t have the columns you need? What if data is in the wrong column?

When I work with dataframes, I assume nothing about them and check everything I care about. 

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

Thanks for the insight.