0

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.

zeawoas
  • 446
  • 7
  • 18
  • Getting all existing instances of a class that exist anywhere in the program is not very useful or encapsulated design, so the language doesn't make it especially easy to do. You might want to `frobnicate()` every Widget that you've created, but you can't be completely sure whether some other module elsewhere in the program created some Widgets too, and doesn't want them to be frobnicated. Better for you to keep all of your widgets in a collection, such as a list or dict, and frobnicate only the members of that collection. This keeps the other module's Widgets safe from your modifications. – Kevin Nov 28 '17 at 15:05

2 Answers2

1

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.

fodma1
  • 3,485
  • 1
  • 29
  • 49
0

Thanks for helping me with your answer and comment.

https://www.youtube.com/watch?v=Opp9AQ90FHw This video explains how to implement list-operations into objects with dunder functions in Python and I thought it might be useful for anybody who is interested in using class-instances to store data.

zeawoas
  • 446
  • 7
  • 18