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 7951607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:36:43+00:00 2026-06-04T02:36:43+00:00

Is there a way to overwrite special functions for built in types in python?

  • 0

Is there a way to overwrite special functions for built in types in python? For example, I want to create a SpecialDict class that inherits from the built in dict. I want to allow users to define custom validation functions for both key and values in my special dictionary, like so:

def __init__(self, keyValidator = True, valueValidator = True):
    self.keyValidator = keyValidator
    self.valueValidator = valueValidator

Using this, I can intercept the addition of values in the update method, like so:

def update(self,key,value):
    assert (self.keyValidator(key))
    assert (self.valueValidator(key))
    self[key] = value

But this won’t work if someone decides to just go ahead and use the [] as access. Or if someone creates the object using a dictionary literal.

mySpecialDict = SpecialDict
mySpecialDict['hello'] = 54
  • 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-06-04T02:36:45+00:00Added an answer on June 4, 2026 at 2:36 am

    Someone can’t create an instance of your SpecialDict as a dictionary literal, because a dictionary literal is just that; a normal dictionary.

    As far as setting items with square bracket notation, as in mySpecialDict['hello'] = 54, that just corresponds to mySpecialDict.__setitem__('hello', 54). Likewise retrieving an item with the square bracket notation corresponds to an invocation of the __getitem__ method. This is true no matter what class mySpecialDict is; whether it’s an ordinary dictionary, a class that subclasses the builtin dict, or some entirely unrelated class. So you can just implement those methods to change what they do (using super(SpecialDict, self).__setitem__(key, value) or dict.__setitem__(self, key, value) to refer to the normal implementation, when you need to).

    One problem you will run into is that some (all?) of the builtin implementation of other dict methods will not respect your overridden __setitem__ or __getitem__. So you won’t be able to inherit them; you’ll have to override all of them and either completely re-implement them in terms of your versions of the basic operations, or at least run your validations “around” the superclass calls.

    A much less painful approach might actually be to not subclass the builtin dict, and instead implement a custom “dict-like” object that wraps an ordinary dictionary, using the collections.Mapping or collections.MutableMapping base classes to get the dictionary interface. Using this approach you’ll only need to implement about 6 basic methods yourself (which you’d do by plugging in your validation logic around calls to the wrapped dictionary), and get sensible definitions of other methods based on them. See http://docs.python.org/library/collections.html#collections-abstract-base-classes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I override class attribute access in python? P.S. Is there a way
Is there way to overwrite the default values for internationalization? Example if I get
I'm wondering is there a way to overwrite Hashtable (or Dictionary) class so it
Is there any way to override a class method with a lambda function? For
Is there a way to override color derived from a CSS class in tag.
Is there a way to override an existing method in a partial class? Something
Is there a way to overwrite the http response headers returned in Jmeter? I'm
I am new to LINQToSQL. Is there a way to overwrite the InsertOnSubmit or
Is there a way in PHP to overwrite a method declared by one interface
Is there a way to set the fonts when a Monotouch Dialog class has

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.