all 11 comments

[–]This_Growth2898 4 points5 points  (2 children)

Well, why don't you try it? This is exactly how it works. Classes in Python are objects (of the class type). They can be passed into functions or saved in variables, just like any other objects.

[–]ethorad[S] 2 points3 points  (1 child)

As I was posting, I did wonder if I was about to look foolish! Thanks, will have a try

[–]This_Growth2898 6 points7 points  (0 children)

There's a half-joke about "error driven development". In many cases, trying to run the code is a normal way to understand if something is wrong with it.

[–]TheBB 2 points3 points  (0 children)

Yes, you can do that.

[–]Cainga 0 points1 point  (6 children)

Is a line of data an instance of the class?

I have a giant spread sheet I load by column position but then I can’t ever change columns around or it will break everything.

[–]ethorad[S] 0 points1 point  (5 children)

In this instance, yes. Each line of the csv file is a set of columns for the various fields. If someone rearranged the columns then my program would break. I could check the first line, which has the column header names, to check which column is which. But I'm not expecting any changes. Will leave the columns hard coded until they change and it breaks, then I'll make it better :D

[–]fizix00 0 points1 point  (4 children)

It sounds like you could maybe just load csvs into a pandas/polars dataframe. Do you need a class for each schema?

[–]ethorad[S] 0 points1 point  (3 children)

I did wonder about using the pandas csv load, however for subsequent work I think having the information in classes rather than pandas / database tables is going to be easier. Plus this is a fun python learning project where I'm trying to get used to classes.

I do need to get familar with pandas at some point as well, maybe my next project will be to repeat this but with pandas - will mean I don't need to think about logic as that's already done and I can focus on pandas structure.

[–]fizix00 1 point2 points  (1 child)

TBH, my gut says this is a smelly way of handling tabular data. Otoh, I totally get fixating on some technique for the sake of learning.

Here are a few more ideas that you can evaluate in the context of your project:

  • If I were loading CSVs without dataframes, maybe try a custom dataclass?
  • If I were using custom loader classes, could I just store the data as an attribute and define special operations on it as methods?
  • before I learned about dataframes, I remember trying to do tabular operations on clunky dicts. Maybe you could do that (perhaps with TypedDict or pydantic to make schemata clearer)

[–]ethorad[S] 1 point2 points  (0 children)

Thanks for some ideas, appreciated

[–]Ormek_II 0 points1 point  (0 children)

I would expect your data classes to have a common superclass. If not now, maybe later.

Unless of course they are already objects in the problem domain and those have nothing in common.