在 Python 中创建自定义 Class 时出现问题
Problem When Creating Custom Class in Python
使用以下代码:
class Int_set(list):
def __init__(self):
self.vals=[]
self.index=0
def insert(self, elm): #assume a string of numbers
for x in elm:
if x not in self.vals:
self.vals.append(int(x))
return self.vals
def member(self, elm):
return elm in self.vals
def remove(self, elm):
try:
self.vals.remove(elm)
except:
raise ValueError(str(elm)+' not found')
def get_members(self):
return self.vals[:]
def __str__(self):
if self.vals == []:
return '[]'
self.vals.sort()
result = ''
for e in self.vals:
result = result + str(e) + ','
return f'{{{result[:-1]}}}'
def union(self, other):
'''add all non-duplicate elements from other set to self set'''
print(len(other))
for e in range(len(other)):
if other[e] not in self.vals:
self.vals.append(other[e])
else:
continue
return self.vals
set1=Int_set()
set1.insert('123456')
print(set1.get_members())
set2=Int_set()
set2.insert('987')
print(set2.get_members())
print(set1.union(set2))
我得到输出:
[1, 2, 3, 4, 5, 6]
[9, 8, 7]
0 # the print(len(other)) in def union
[1, 2, 3, 4, 5, 6]
请注意 def union(self, other) 并没有添加从 set2 到 set1 的所有唯一数字。但是,如果我使用:
print(set1.union(set2.vals))
然后我得到:
[1, 2, 3, 4, 5, 6]
[9, 8, 7]
3 # the print(len(other)) in def union
[1, 2, 3, 4, 5, 6, 9, 8, 7]
这是什么原因造成的?我假设 .union(set2)
中的 set2 处于初始化时的状态(因此它是一个空列表)。但是我在里面插入了一些数字并打印出来
您在 Int_set
的实例上调用 len
。这是从 list
派生的,并为您提供实例本身的长度,即 0
。您需要在 class 中覆盖 __len__
并返回实例 val
.
的值
def __len__(self):
return len(self.vals)
而且您还必须更改 union
方法。现在你遍历 Int_set
实例的成员,有 none。您必须遍历 vals
属性。
def union(self, other):
'''add all non-duplicate elements from other set to self set'''
print(len(other))
for element in other.vals:
if element not in self.vals:
self.vals.append(element)
else:
continue
return self.vals
总体而言,不清楚为什么您决定将 Int_set
设为 list
的子 class,因为您没有使用 list
class 的任何功能。
您的问题是您试图从 class 本身读取值,您使用的是 other
而不是 other.vals
。当您尝试
时,您已经回答了您的问题
print(set1.union(set2.vals))
所以你可以把你的函数改成这样:
def union(self, other):
'''add all non-duplicate elements from other set to self set'''
print(len(other.vals))
for e in range(len(other.vals)):
if other.vals[e] not in self.vals:
self.vals.append(other.vals[e])
else:
continue
return self.vals
使用以下代码:
class Int_set(list):
def __init__(self):
self.vals=[]
self.index=0
def insert(self, elm): #assume a string of numbers
for x in elm:
if x not in self.vals:
self.vals.append(int(x))
return self.vals
def member(self, elm):
return elm in self.vals
def remove(self, elm):
try:
self.vals.remove(elm)
except:
raise ValueError(str(elm)+' not found')
def get_members(self):
return self.vals[:]
def __str__(self):
if self.vals == []:
return '[]'
self.vals.sort()
result = ''
for e in self.vals:
result = result + str(e) + ','
return f'{{{result[:-1]}}}'
def union(self, other):
'''add all non-duplicate elements from other set to self set'''
print(len(other))
for e in range(len(other)):
if other[e] not in self.vals:
self.vals.append(other[e])
else:
continue
return self.vals
set1=Int_set()
set1.insert('123456')
print(set1.get_members())
set2=Int_set()
set2.insert('987')
print(set2.get_members())
print(set1.union(set2))
我得到输出:
[1, 2, 3, 4, 5, 6]
[9, 8, 7]
0 # the print(len(other)) in def union
[1, 2, 3, 4, 5, 6]
请注意 def union(self, other) 并没有添加从 set2 到 set1 的所有唯一数字。但是,如果我使用:
print(set1.union(set2.vals))
然后我得到:
[1, 2, 3, 4, 5, 6]
[9, 8, 7]
3 # the print(len(other)) in def union
[1, 2, 3, 4, 5, 6, 9, 8, 7]
这是什么原因造成的?我假设 .union(set2)
中的 set2 处于初始化时的状态(因此它是一个空列表)。但是我在里面插入了一些数字并打印出来
您在 Int_set
的实例上调用 len
。这是从 list
派生的,并为您提供实例本身的长度,即 0
。您需要在 class 中覆盖 __len__
并返回实例 val
.
def __len__(self):
return len(self.vals)
而且您还必须更改 union
方法。现在你遍历 Int_set
实例的成员,有 none。您必须遍历 vals
属性。
def union(self, other):
'''add all non-duplicate elements from other set to self set'''
print(len(other))
for element in other.vals:
if element not in self.vals:
self.vals.append(element)
else:
continue
return self.vals
总体而言,不清楚为什么您决定将 Int_set
设为 list
的子 class,因为您没有使用 list
class 的任何功能。
您的问题是您试图从 class 本身读取值,您使用的是 other
而不是 other.vals
。当您尝试
print(set1.union(set2.vals))
所以你可以把你的函数改成这样:
def union(self, other):
'''add all non-duplicate elements from other set to self set'''
print(len(other.vals))
for e in range(len(other.vals)):
if other.vals[e] not in self.vals:
self.vals.append(other.vals[e])
else:
continue
return self.vals