Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:08:01+00:00 2026-05-27T21:08:01+00:00

Can someone explain what the dict class is used for? This snippet is from

  • 0

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

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

I understand the assignment of key=value pairs with self['name'] = filename but what does inheriting the dict class have to do with this? Please help me understand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:08:01+00:00Added an answer on May 27, 2026 at 9:08 pm

    It’s for creating your own customized Dictionary type.

    You can override __init__, __getitem__ and __setitem__ methods for your own special purposes to extend dictionary’s usage.

    Read the next section in the Dive into Python text: we use such inheritance to be able to work with file information just the way we do using a normal dictionary.

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

    The fileinfo class is designed in a way that it receives a file name in its constructor, then lets the user get file information just the way you get the values from an ordinary dictionary.

    Another usage of such a class is to create dictionaries which control their data. For example you want a dictionary who does a special thing when things are assigned to, or read from its ‘sensor’ key. You could define your special __setitem__ function which is sensitive with 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 each time user reads the ‘temperature’ key. For this you subclass a __getitem__ function:

    def __getitem__(self, key):
        if key == "temperature":
            return CurrentWeatherTemperature()
        else:
            return self.data[key]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the last line of this Python code snippet to me? Cell
Can someone explain clearly why this implementation (from SO 3965054) of min_of_list works in
Can someone explain why this code doesn't work? Intent i = new Intent(this.context, SomeClass.class);
can someone explain how this hexadecimal view of the .o file in xvi32 corresponds
Can someone explain what this excerpt from Single Page Apps in Depth means? A
Can someone explain this behavior for me? mapping = dict.fromkeys([1, 2, 3], []) objects
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in
Can someone explain why, in this situation, scandir is getting my directory, but the
Can someone explain step by step how this query is processed? SELECT F.ID, F.FirstName,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.