Classes vs Dictionaries
I am planning on making a program that has about 15 items, each with 5 of the same attributes each. Essentially it will function like a mini database. I plan to do this in python and I am wondering whether I should create 15 dictionaries, each with 5 key:value pairs or create 15 classes, each with 5 attributes assigned to objects. Which would be best for this task? And generally, when should you use one over the other?
9/8/2018 4:29:44 PMRob Bailiff
5 AnswersNew Answer
Maninder Singh Thanks for that link. I think the key the answer that states "A class is really just a dictionary where some of the keys are functions that automatically take the whole dictionary as the first argument". It's a good way to think of it.
Matthias Yeah, it sounds like making one class and then 15 instances makes more sense. I think each object would have a lot of code to initialise but that would be less than the code 15 classes and then initialising 15 objects. Thanks 👍
The advantage of classes would be that you can "glue" specific functions to them and also inherit from base classes if you want to store more stuff in it and use more functions. So the are more flexible in some sense if you want to improve your program. Also you don't need 15 different classes, one is enough. Then make 15 instances of it. Maybe it is also usefull to combine it and use a dict in your class or use a dict to store the class instances. It really depends on what you exactly want to do. In my opinion: Just try with whatever you feel good with, and if you notice that another data structure would fit better, code it in another style for practice. Otherwise, if you know more about future goals or functionalities about your software you want to create, analyze it and choose the most fitting according to your plans.
here is answer check this. https://www.google.co.in/amp/s/amp.reddit.com/r/learnpython/comments/2xv0qg/classes_vs_dictionary/
Class is in objects dictionaries in data