Python Advanced Programming (class-level): Subclass built-in type

Source: Internet
Author: User

#-*-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.

"""

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.