Dict is functionally nearly equivalent with the builtin Python dict, but provides the following extras: - Items can be accessed with attribute syntax as well as dictionary syntax. Thus, if C is a Dict, the following are equivalent: C['foo'] or C.foo This works as well for accessing values as for setting values. In the following, the words key or attribute therefore have the same meaning. - Lookup of a nonexisting key/attribute does not automatically raise an error, but calls a _default_ lookup method which can be set by the user. The default is to raise a KeyError, but an alternative is to return None or some other default value.
There are a few caveats though: - Keys that are also attributes of the builtin dict type, can not be used with the attribute syntax to get values from the Dict. You should use the dictionary syntax to access these items. It is possible to set such keys as attributes. Thus the following will work: C['get'] = 'foo' C.get = 'foo' print C['get'] but not print C.get
This is done so because we want all the dict attributes to be available with their normal binding. Thus, print C.get('get') will print foo
To avoid name clashes with user defines, many Python internal names start and end with '__'. The user should avoid such names. The Python dict has the following attributes not enclosed between '__', so these are the one to watch out for: 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'.
The Dict class has this constructor:
data=,default=None) |
The Dict can be initialized with a Python dict or a Dict. If defined, default is a function that is used for alternate key lookup if the key was not found in the dict.
Dict objects have the following methods:
) |
We use the format Dict(), so that the string is a valid Python representation of the Dict.
key) |
This is equivalent to the dict lookup, except that we provide a default value if the key does not exist.
key) |
Silently ignore if key is nonexistant.
key) |
This makes self.key equivalent to self['key'], except if key is an attribute of the builtin type 'dict': then we return that attribute instead, so that the 'dict' methods keep their binding.
key,value=None) |
This works even if the key is an existing attribute of the builtin dict class: the key,value pair is stored in the dict, leaving the dict's attributes unchanged.
key) |
This works even if the key is an existing attribute of the builtin dict class: the item is deleted from the dict, leaving the dict's attributes unchanged.
data=) |
The data can be a dict or Dict type object.
key,default) |
This is the equivalent of the dict get method, except that it returns only the default value if the key was not found in self, and there is no _default_ method or it raised a KeyError.
key,default) |
This is the same as the get method, except that it also sets the default value if get found a KeyError.
memo) |
) |
state) |