无法将值附加到 Python 中的列表?
Cannot append value to list in Python?
我正在创建自定义 class mylist
,它继承自 python 中的 list
。
但我无法附加值
class mylist(list):
def __init__(self,max_length):
super().__init__()
self.max_length = max_length
def append(self, *args, **kwargs):
""" Append object to the end of the list. """
if len(self) > self.max_length:
raise Exception
a = mylist(5)
a.append(5)
print(a)
# output
#[]
您定义了自己的 append()
,因此您替换了原来的 append()
并且它没有向列表添加元素。
您可以使用 super()
到 运行 原始 append()
def append(self, *args, **kwargs):
""" Append object to the end of the list. """
if len(self) > self.max_length:
raise Exception
if args:
super().append(args[0])
我认为你应该使用 >=
而不是 >
因为你在添加项目之前检查它。如果你想在添加项目后检查它,你可以使用 >
顺便说一句:
因为您使用 *args
,所以您可以使用 for
-loop 来追加可能的值 a.append(5, 6, 7)
。原来append()
这个不行
for item in args:
super().append(item)
可能需要检查 len(self) + len(args) > self.max_length
或者可能需要在 for
-loop
中检查 len(self)
您还可以检查 args
是否有任何值,并在 运行 a.append()
没有任何值时引发错误。
class MyList(list): # PEP8: `CamelCaseNames` for classes
def __init__(self, max_length):
super().__init__()
self.max_length = max_length
def append(self, *args, **kwargs):
""" Append object to the end of the list. """
#if len(self) >= self.max_length:
# raise Exception
#if args:
# super().append(args[0])
if not args: # use `TypeError` like in `list.append()`
raise TypeError("descriptor 'append' of 'list' object needs an argument")
for item in args:
if len(self) >= self.max_length:
raise Exception
super().append(item)
def insert(self, pos, value):
if len(self) >= self.max_length:
raise Exception
super().insert(pos, value)
# --- main ---
#list.append() # raise `TypeError`
a = MyList(5)
try:
a.append() # raise `TypeError` like in `list.append()`
except Exception as ex:
print('ex:', ex)
a.append(5)
print(a) # [5]
a.append(6, 7, 8, 9)
print(a) # [5, 6, 7, 8, 9]
a.append(10) # raise ERROR
a.insert(0, 999) # raise ERROR
我正在创建自定义 class mylist
,它继承自 python 中的 list
。
但我无法附加值
class mylist(list):
def __init__(self,max_length):
super().__init__()
self.max_length = max_length
def append(self, *args, **kwargs):
""" Append object to the end of the list. """
if len(self) > self.max_length:
raise Exception
a = mylist(5)
a.append(5)
print(a)
# output
#[]
您定义了自己的 append()
,因此您替换了原来的 append()
并且它没有向列表添加元素。
您可以使用 super()
到 运行 原始 append()
def append(self, *args, **kwargs):
""" Append object to the end of the list. """
if len(self) > self.max_length:
raise Exception
if args:
super().append(args[0])
我认为你应该使用 >=
而不是 >
因为你在添加项目之前检查它。如果你想在添加项目后检查它,你可以使用 >
顺便说一句:
因为您使用 *args
,所以您可以使用 for
-loop 来追加可能的值 a.append(5, 6, 7)
。原来append()
这个不行
for item in args:
super().append(item)
可能需要检查 len(self) + len(args) > self.max_length
或者可能需要在 for
-loop
len(self)
您还可以检查 args
是否有任何值,并在 运行 a.append()
没有任何值时引发错误。
class MyList(list): # PEP8: `CamelCaseNames` for classes
def __init__(self, max_length):
super().__init__()
self.max_length = max_length
def append(self, *args, **kwargs):
""" Append object to the end of the list. """
#if len(self) >= self.max_length:
# raise Exception
#if args:
# super().append(args[0])
if not args: # use `TypeError` like in `list.append()`
raise TypeError("descriptor 'append' of 'list' object needs an argument")
for item in args:
if len(self) >= self.max_length:
raise Exception
super().append(item)
def insert(self, pos, value):
if len(self) >= self.max_length:
raise Exception
super().insert(pos, value)
# --- main ---
#list.append() # raise `TypeError`
a = MyList(5)
try:
a.append() # raise `TypeError` like in `list.append()`
except Exception as ex:
print('ex:', ex)
a.append(5)
print(a) # [5]
a.append(6, 7, 8, 9)
print(a) # [5, 6, 7, 8, 9]
a.append(10) # raise ERROR
a.insert(0, 999) # raise ERROR