Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use class versus dict in python? [closed]

Tags:

python

When considering design, in which cases does it make sense to use class versus dict? Pros & Cons would be useful too.

For example,

class AlbumState:     """ AlbumState class, tracks photos shown, etc"""      def __init__ (self, album):         """ album for this object will track state"""         self.album = album          self.photos_shown = []         self.photos_notshown = range(album.size()) 

versus

albumstate['album'] = album albumstate['photos_shown'] = [] albumstate['photos_notshown'] = range(album.size()) 
like image 459
amitc Avatar asked Jun 19 '12 19:06

amitc


People also ask

Why use a dictionary instead of a class Python?

Classes are for bundling related data (and usually code). Dictionaries are for storing key-value relationships, where usually the keys are all of the same type, and all the values are also of one type.

What's the difference between class and dictionary in Python?

A dictionary is an arbitrary mapping. An object is a special mapping from names to variables and methods. A class is a language construct that gathers together objects with similar structure and helps to create objects. Objects and classes can be simulated in a straightforward way using functions and dictionaries.

Should I use dict () or {}?

With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.

When should you not use a dictionary Python?

Python dictionaries can be used when the data has a unique reference that can be associated with the value. As dictionaries are mutable, it is not a good idea to use dictionaries to store data that shouldn't be modified in the first place.


1 Answers

I would say the first and most important criteria to distinguish whether to use a class or a dictionary is whether you just want to have some data storage or you also want to have some logic (i.e., methods).

If you only need to keep together several data, then a dictionary might be the way to go. On the other hand, if you need operations performed on that data, and there is a very tight relationship between the data and the operations (forming a kind of entity), then that really calls for using a class.

As @Ben is suggesting, you may start using just a dictionary when you have a bunch of related data, and if at some point your realize that you also need some logic for that data, you can turn the dictionary into a class.

You may also have a look at the Unified Modeling Language (UML) and its class diagrams to get a better feeling on how to use classes. If you are just doing some scripting or programming a small application you may not need to use UML, but if you are designing a bigger and more complex system, it can really help you to decide what and how many classes do you need beforehand.

like image 180
betabandido Avatar answered Oct 24 '22 01:10

betabandido