|
3 | 3 |
|
4 | 4 | class BaseDict(dict): |
5 | 5 |
|
6 | | - _dict = {} |
| 6 | + _dict = None |
7 | 7 |
|
8 | 8 | def __init__(self, *args, **kwargs): |
9 | 9 | if len(args) == 1 and isinstance(args[0], dict): |
10 | 10 | self._dict = args[0] |
11 | | - else: |
12 | | - self._dict = dict(*args, **kwargs) |
13 | | - super(BaseDict, self).__init__(self._dict) |
14 | | - |
15 | | - def __bool__(self): |
16 | | - return bool(self._dict) |
| 11 | + super(BaseDict, self).__init__(self._dict) |
| 12 | + return |
| 13 | + self._dict = None |
| 14 | + super(BaseDict, self).__init__(*args, **kwargs) |
17 | 15 |
|
18 | 16 | def __contains__(self, key): |
19 | | - return key in self._dict |
| 17 | + if self._dict is not None: |
| 18 | + return key in self._dict |
| 19 | + return super(BaseDict, self).__contains__(key) |
20 | 20 |
|
21 | 21 | def __delitem__(self, key): |
22 | | - del self._dict[key] |
| 22 | + if self._dict is not None: |
| 23 | + del self._dict[key] |
| 24 | + return |
23 | 25 | super(BaseDict, self).__delitem__(key) |
24 | 26 |
|
25 | 27 | def __eq__(self, other): |
26 | | - return self._dict == other |
| 28 | + if self._dict is not None: |
| 29 | + return self._dict == other |
| 30 | + return super(BaseDict, self).__eq__(other) |
27 | 31 |
|
28 | 32 | def __getitem__(self, key): |
29 | | - return self._dict[key] |
| 33 | + if self._dict is not None: |
| 34 | + return self._dict[key] |
| 35 | + return super(BaseDict, self).__getitem__(key) |
30 | 36 |
|
31 | 37 | def __iter__(self): |
32 | | - return iter(self._dict) |
| 38 | + if self._dict is not None: |
| 39 | + return iter(self._dict) |
| 40 | + return super(BaseDict, self).__iter__() |
33 | 41 |
|
34 | 42 | def __len__(self): |
35 | | - return len(self._dict) |
36 | | - |
37 | | - def __nonzero__(self): |
38 | | - return bool(self._dict) |
| 43 | + if self._dict is not None: |
| 44 | + return len(self._dict) |
| 45 | + return super(BaseDict, self).__len__() |
39 | 46 |
|
40 | 47 | def __repr__(self): |
41 | | - return repr(self._dict) |
| 48 | + if self._dict is not None: |
| 49 | + return repr(self._dict) |
| 50 | + return super(BaseDict, self).__repr__() |
42 | 51 |
|
43 | 52 | def __setitem__(self, key, value): |
44 | | - self._dict[key] = value |
| 53 | + if self._dict is not None: |
| 54 | + self._dict[key] = value |
| 55 | + return |
45 | 56 | super(BaseDict, self).__setitem__(key, value) |
46 | 57 |
|
47 | 58 | def __str__(self): |
48 | | - return str(self._dict) |
| 59 | + if self._dict is not None: |
| 60 | + return str(self._dict) |
| 61 | + return super(BaseDict, self).__str__() |
49 | 62 |
|
50 | 63 | def __unicode__(self): |
51 | | - return unicode(self._dict) |
| 64 | + if self._dict is not None: |
| 65 | + return unicode(self._dict) |
| 66 | + return super(BaseDict, self).__unicode__() |
52 | 67 |
|
53 | 68 | def clear(self): |
54 | | - self._dict.clear() |
| 69 | + if self._dict is not None: |
| 70 | + self._dict.clear() |
| 71 | + return |
55 | 72 | super(BaseDict, self).clear() |
56 | 73 |
|
57 | 74 | def copy(self): |
58 | | - return self._dict.copy() |
| 75 | + if self._dict is not None: |
| 76 | + return self._dict.copy() |
| 77 | + return super(BaseDict, self).copy() |
59 | 78 |
|
60 | 79 | def dict(self): |
61 | | - return self._dict |
| 80 | + if self._dict is not None: |
| 81 | + return self._dict |
| 82 | + return self |
62 | 83 |
|
63 | 84 | def get(self, key, default=None): |
64 | | - return self._dict.get(key, default) |
| 85 | + if self._dict is not None: |
| 86 | + return self._dict.get(key, default) |
| 87 | + return super(BaseDict, self).get(key, default) |
65 | 88 |
|
66 | 89 | def items(self): |
67 | | - return self._dict.items() |
| 90 | + if self._dict is not None: |
| 91 | + return self._dict.items() |
| 92 | + return super(BaseDict, self).items() |
68 | 93 |
|
69 | 94 | def keys(self): |
70 | | - return self._dict.keys() |
| 95 | + if self._dict is not None: |
| 96 | + return self._dict.keys() |
| 97 | + return super(BaseDict, self).keys() |
71 | 98 |
|
72 | 99 | def pop(self, key, *args): |
73 | | - value = self._dict.pop(key, *args) |
74 | | - super(BaseDict, self).pop(key, None) |
75 | | - return value |
| 100 | + if self._dict is not None: |
| 101 | + return self._dict.pop(key, *args) |
| 102 | + return super(BaseDict, self).pop(key, *args) |
76 | 103 |
|
77 | 104 | def setdefault(self, key, default=None): |
78 | | - value = self._dict.setdefault(key, default) |
79 | | - super(BaseDict, self).setdefault(key, default) |
80 | | - return value |
| 105 | + if self._dict is not None: |
| 106 | + return self._dict.setdefault(key, default) |
| 107 | + return super(BaseDict, self).setdefault(key, default) |
81 | 108 |
|
82 | 109 | def update(self, other): |
83 | | - self._dict.update(other) |
| 110 | + if self._dict is not None: |
| 111 | + self._dict.update(other) |
| 112 | + return |
84 | 113 | super(BaseDict, self).update(other) |
85 | 114 |
|
86 | 115 | def values(self): |
87 | | - return self._dict.values() |
| 116 | + if self._dict is not None: |
| 117 | + return self._dict.values() |
| 118 | + return super(BaseDict, self).values() |
0 commit comments