Python_ overriding collections

Source: Internet
Author: User

Class Set (object):
def __init__ (Self,data=none):
if data = = None:
Self.__data = []
Else
If not hasattr (data, ' __iter__ '):
#提供的数据不可以迭代, instantiation failed
Raise Exception (' must provide an iterative data type ')
temp = []
For item in data:
#集合中的元素必须是可哈希
Hash (item)
If not item in temp:
Temp.append (item)
Self.__data = Temp

#析构函数
def __del__ (self):
Del Self.__data

#添加元素, the requirement element must be hashed
def add (self, Other):
Hash (Other)
If and not in Self.__data:
Self.__data.append (Other)
Else
Print (' element already exists, operation is ignored ')

#删除元素
def remove (Self,other):
If and in Self.__data:
Self.__data.remove (Other)
Print (' delete succeeded ')
Else
Print (' element not present, delete operation ignored ')

#随机弹出并返回一个元素
def pop (self):
If not self.__dat:
Print (' collection is empty, popup operation ignored ')
Return
Import Random
Item = Random.choice (Self.__data)
Self.__data.remove (item)
Return item

#运算符重载, set differential set operation
def __sub__ (self, Other):
If not isinstance (Other,set):
Raise Exception (' type error ')
#空集合
result = Set ()
#如果一个元素属于当前集合而不属于另一个集合, add
For item in Self.__data:
If Item not in Other.__data:
Result.__data.append (item)
return result

#提供方法, set differential set operation, reuse the above code
def difference (self,other):
Return Self-other

#| operator overloading, set merge set operation
def __or__ (self, Other):
If not isinstance (Other,set):
Raise Exception (' type error ')
result = Set (self.__data)
For item in Other.__data:
If Item not in Result.__data:
Result.__data.append (item)
return result

#提供方法, set merge set operations
Def union (Self,otherset):
return Self | Otherset

#& operator overloading, set intersection operation
def __and__ (self, Other):
If not isinstance (Other,set):
Raise Exception (' type error ')
result = Set ()
For item in Self.__data:
If item in Other.__data:
Result.__data.append (item)
return result

#^ operator overloading, set symmetric difference set
def __xor__ (self, Other):
Return (Self-other) | (other-self)

#提供方法, set symmetric differential set operation
def symetric_difference (Self,other):
Return self ^ Other

#== operator overload to determine whether two sets are equal
def __eq__ (self, Other):
If not isinstance (Other,set):
Raise Exception (' type error ')
If sorted (self.__data) = = Sorted (other.__data):
Return True
Return False

#> operator overloading, collection contains relationships
def __gt__ (self, Other):
If not isinstance (Other,set):
Raise Exception (' type error ')
If self! = Other:
Flag1 = True
For item in Self.__data:
If Item not in Other.__data:
#当前集合中有的元素不属于另一个集合
Flag1 = False
Break
Flag2 = True
For item in Other.__data:
If Item not in Self.__data:
#另一集合中的元素不属于当前集合
Flag2 = False
Break
If not Flag1 and Flag2:
Return True
Return False

#>= operator overloading, collection contains relationships
def __ge__ (self, Other):
If not isinstance (Other,set):
Raise Exception (' type error ')
return self = = other or self > Other

#提供方法 to determine whether the current collection is a true subset of another collection
def issubset (Self,other):
Return Self<other

#提供方法 to determine whether the current collection is a superset of another collection
def issuperset (Self,other):
return self > Other

#提供方法, clears all elements of the collection
def clear (self):
While Self.__data:
Del Self.__data[-1]
Print (' Collection emptied ')

#运算符重载 so that the collection can iterate
def __iter__ (self):
Return iter (Self.__data)

#运算符重载, the in operator is supported
def __contains__ (self, item):
Return item in Self.__data

#支持内置函数len ()
def __len__ (self):
Return Len (self.__data)

#直接查看该类对象时调用该函数
def __repr__ (self):
Return ' {' +str (self.__data) [1:-1]+ '} '

This function is called when the #使用print () function outputs the class object
__str__ = __repr__

Python_ overriding collection

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.