https://www.youtube.com/watch?v=ZDa-Z5JzLYM&list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc
In this video series on OOP the Python-youtuber Corey Schafer uses instances of his Employee class to store data on employees like their salary and the coding language they know etc. I am rather new to 'higher level' programming and wanted to implement this in one of my projects where I also had to store and subsequently alter the attributes of many similar objects. Soon I realized, however, that it is rather cumbersome to handle the data after the instances have been constructed. For example, how can I iterate over all instances of a certain class or subclass or find instances that have a certain value for a specific attribute? Am I missing something big here or are there really no convenient ways to access the data this way? Finally I ended up using an extra dictionary that stored the instance names and an identifier number to somehow be able to read through the data but then I just could have used a dictionary in the first place. So, is the usage of class instances as data structure like it is displayed in this youtube series just a bad example on how to use classes/instances etc. or is there more to it than the videos show? I have read quite a bit about the topic but still can't figure out why you wouldn't use a dict in the first place or why fundamental built-in functions that would allow to e.g. easily fetch all instances of a subclass are lacking.
Maybe someone can shed some light upon these issues and untangle my confusion. Thanks.
What you're looking for is a whole different topic IMO. In general, you should be able to achieve such behavior with a proper ORM. I can recommend you looking into the Django ORM or SQL alchemy where the individual data entities are indeed class instances. Instances and classes are often described with cars/employees, students, etc. There's nothing wrong with this description, the way you store your data is up to you. In general, you should be able to store your data in a list and filter on it:
employees = [
Emplyee(...),
Emplyee(...),
Emplyee(...),
...
]
employees_with_high_salary = [e for e in employees if e.salary > 400]
In general, Python classes do not keep track of their instances, unless you do so. So you are able to achieve this, and you should be able to store all your values indexed, but Python won't do that for you and it's not the expected behavior.