What is the dict class used for

Can someone explain what the dict class is for? This snippet from Dive Into Python

 class FileInfo(dict): "store file metadata" def __init__(self, filename=None): self["name"] = filename 

I understand the purpose of the key = value pairs with self['name'] = filename , but what does the dict class inherit from? Please help me understand.

+4
source share
3 answers

It is designed to create your own custom dictionary type.

You can override the __init__ , __getitem__ and __setitem__ methods for your special purposes to expand the use of the dictionary.

Read the next section in the Dive text in Python: we use such inheritance to be able to work with file information in the same way as we use a regular dictionary.

 # From the example on the next section >>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3") >>> f["name"] '/music/_singles/kairo.mp3' 

The fileinfo class fileinfo designed in such a way that it receives the file name in its constructor, and then allows the user to get information about the file only as you get values ​​from a regular dictionary.

Another use of this class is to create dictionaries that control their data. For example, you need a dictionary that does a particular thing when something is assigned, or read its "sensor" key. You can define a special function __setitem__ that is sensitive to the key name:

 def __setitem__(self, key, item): self.data[key] = item if key == "sensor": print("Sensor activated!") 

Or, for example, you want to return a special value every time the user reads "temperature". To do this, you subclass the __getitem__ function:

 def __getitem__(self, key): if key == "temperature": return CurrentWeatherTemperature() else: return self.data[key] 
+1
source

If you are not familiar with the concept of object-oriented programming inheritance, take a look at at least this wiki article (although, just for the introduction, it may not be the best).

In python, we use this syntax to define class A as a subclass of class B :

 class A(B): pass # empty class 

In your example, since the FileInfo class inherits from the standard dict type, you can use instances of this class as dictionaries (as they have all the methods that have a regular dict object). Among other things that allow you to assign values ​​this way ( dict provides a method for passing this operation):

 self['name'] = filename 

Is this an explanation that you want or you don’t understand something else?

+3
source

When a class in Python inherits from another class, this means that any of the methods defined in the inherited class is inherently defined for the newly created class.

Therefore, when FileInfo inherits a dict , this means that all functions of the dict class are now available for FileInfo , in addition to all that FileInfo can declare or, more importantly, override the re-definition of a method or parameter.

Since the dict Object in Python allows key / value pairs, this allows FileInfo have access to the same mechanism.

+1
source

Source: https://habr.com/ru/post/1389108/


All Articles