#-*-Coding:utf-8-*-
# python:2.x
__author__ = ' Administrator '
#类级
#在2.2, the type is presented (TYPE0 and Class) are unified (visit: Https://www.python.org/download/releases/2.2.3/descintro (may no longer exist) )-This makes the subclass of the built-in type possible, and adds a new built-in type Object
#用于所有内建类的公共祖先
#展示一个名为distinctdict类的代码, as with normal dict, but it does not allow multiple identical key values, ValueError exception occurs when the same item occurs.
Class Distincterror (Exception):
Pass
Class Distinctdict (Dict):
def __setitem__ (self, Key, value):
Try
Value_index=self.values (). Index (value)
#只要dict在2次调用之间没有发生改变
#keys () and values () will return to the corresponding list
#否则, the Dict type cannot guarantee the order of the sequence
Existing_key=self.keys () [Value_index]
If Existing_key!=key:
Raise Distincterror (' This value already exists for%s: '% (str (Self[existing_key])))
Except ValueError as S:
Pass
Super (Distinctdict,self) __setitem__ (Key,value)
My=distinctdict ()
my[' key ']= ' value '
#my [' Other_key ']= ' value ' __main__. Distincterror:this value already exists for value:
my[' Other_key ']= ' value2 '
Print my#{' other_key ': ' value2 ', ' key ': ' Value '}
#看看刚刚的代码, it will be discovered by implementing built-in types, which are like sub-types, faster and clearer
#例如
Class F (list):
def __init__ (self,a):
Self.a=a
def dir (self):
print ' f.%s '%SELF.A
For I in self:
Print I
F=f (' secret ')
Print F
F.append (' pics ')
F.append (' Apply ')
F.dir ()
#其他
"""
Starting with 2.4, the collections module has provided some useful container types that can be used to implement
1: Implement the Deque type of the double-ended queue
The 2:defaultdict type provides a dictionary-like object with default unknown key values, similar to how hash works in Prel or Ruby
----
Built-in types cover most usage scenarios
If you open create a similar sequence or map a new class, you should test its properties and observe the existing built-in types.
"""