Task: Create an object_Native demonstration


C++: #include <iostream>
#include <map>
#include <utility>

using namespace std;

template<typename T>
class FixedMap : private T
{
    
    
    
    
    
    T m_defaultValues;
    
public:
    FixedMap(T map)
    : T(map), m_defaultValues(move(map)){}
    
    
    using T::cbegin;
    using T::cend;
    using T::empty;
    using T::find;
    using T::size;

    
    using T::at;
    using T::begin;
    using T::end;
    
    
    
    auto& operator[](typename T::key_type&& key)
    {
        
        return this->at(forward<typename T::key_type>(key));
    }
    
    
    
    void erase(typename T::key_type&& key)
    {
        T::operator[](key) = m_defaultValues.at(key);
    }

    
    void clear()
    {
        
        T::operator=(m_defaultValues);
    }
    
};


auto PrintMap = [](const auto &map)
{
    for(auto &[key, value] : map)
    {
        cout << "{" << key << " : " << value << "} ";
    }
    cout << "\n\n";
};

int main(void) 
{
    
    cout << "Map intialized with values\n";
    FixedMap<map<string, int>> fixedMap ({
        {"a", 1},
        {"b", 2}});
    PrintMap(fixedMap);
    
    cout << "Change the values of the keys\n";
    fixedMap["a"] = 55;
    fixedMap["b"] = 56;
    PrintMap(fixedMap);
    
    cout << "Reset the 'a' key\n";
    fixedMap.erase("a");
    PrintMap(fixedMap);
    
    cout << "Change the values the again\n";
    fixedMap["a"] = 88;
    fixedMap["b"] = 99;
    PrintMap(fixedMap);
    
    cout << "Reset all keys\n";
    fixedMap.clear();
    PrintMap(fixedMap);
  
    try
    {
        
        cout << "Try to add a new key\n";
        fixedMap["newKey"] = 99;
    }
    catch (exception &ex)
    {
        cout << "error: " << ex.what();
    }
}






Python: from collections import UserDict
import copy

class Dict(UserDict):
    
    def __init__(self, dict=None, **kwargs):
        self.__init = True
        super().__init__(dict, **kwargs)
        self.default = copy.deepcopy(self.data)
        self.__init = False
    
    def __delitem__(self, key):
        if key in self.default:
            self.data[key] = self.default[key]
        else:
            raise NotImplementedError

    def __setitem__(self, key, item):
        if self.__init:
            super().__setitem__(key, item)
        elif key in self.data:
            self.data[key] = item
        else:
            raise KeyError

    def __repr__(self):
        return "%s(%s)" % (type(self).__name__, super().__repr__())
    
    def fromkeys(cls, iterable, value=None):
        if self.__init:
            super().fromkeys(cls, iterable, value)
        else:
            for key in iterable:
                if key in self.data:
                    self.data[key] = value
                else:
                    raise KeyError

    def clear(self):
        self.data.update(copy.deepcopy(self.default))

    def pop(self, key, default=None):
        raise NotImplementedError

    def popitem(self):
        raise NotImplementedError

    def update(self, E, **F):
        if self.__init:
            super().update(E, **F)
        else:
            haskeys = False
            try:
                keys = E.keys()
                haskeys = Ture
            except AttributeError:
                pass
            if haskeys:
                for key in keys:
                    self[key] = E[key]
            else:
                for key, val in E:
                    self[key] = val
            for key in F:
                self[key] = F[key]

    def setdefault(self, key, default=None):
        if key not in self.data:
            raise KeyError
        else:
            return super().setdefault(key, default)