python运算符重载编程__ne__怎么用_Python运算符重载学习

#类SingleList

class SingleList:

def __init__(self, initialList = None):

'''Initialized SingleList instance'''

self.__list = []

if initialList:

for value in initialList:

if value not in self.__list:

self.__list.append(value)

def __str__(self):

'''Overlaoded string representation'''

tempString = ""

i = 0

for i in range(len(self)):

tempString += "%12d" % self.__list[i]

if((i+1) % 4 == 0):

tempString += "/n"

if i % 4 != 0:

tempString += "/n";

return tempString

def __len__(self):

'''Overload length of the list'''

return len(self.__list)

def __getitem__(self, index):

'''Overload sequence element access'''

return self.__list[index]

def __setitem__(self, index, value):

'''Overload sequence element set'''

if value in self.__list:

raise ValureError

self.__list[index] = value

def __eq__(self, other):

'''Overload equality operator'''

if len(self) != len(other):

return 0

for i in range(len(self)):

if self.__list[i] != other.__list[i]:

return 0

return 1

def __ne__(self, other):

'''Overload != and <> operators'''

return not(self == other)

#类SingleList的驱动程序

def readInput():

'''read input'''

size = int(input("List size:"))

readList = []

for i in range(size):

readList.append(int(input()))

return readList

list1 = SingleList(readInput())

list2 = SingleList(readInput())

print("length of list1 %d" % (len(list1)))

print("length of list2 %d" % (len(list2)))

if list1 == list2:

print("equal")

if list1 != list2:

print("Not equal")

print(str(list1))

print("the 1st element in list1 is:", list1[0])

list1[0] = 100

print("after assign, the 1st element in list1 is:", list1[0])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值