Skip to content

Commit 664fcb7

Browse files
committed
增加了List对象,是对Python原版列表的扩展
1 parent 0f8b83c commit 664fcb7

File tree

1 file changed

+116
-3
lines changed

1 file changed

+116
-3
lines changed

type.py

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
对Python原版类型的扩展
33
"""
44
__author__ = "Jerry"
5-
__version__ = "1.2.6"
5+
__version__ = "1.3.6"
6+
7+
__all__ = ["String", "Integer", "List"]
68

79
class NotComposedOfNumbersError(Exception):
8-
"不是由数字组成的字符串使用String对象的toInt()函数或toInteger()函数时抛出"
10+
"不是由数字组成的字符串使用某些方法时抛出"
911
pass
1012

1113
class String:
@@ -246,4 +248,115 @@ def toStr(self):
246248

247249
def toString(self):
248250
"将整数转成String对象"
249-
return String(str(self.__int))
251+
return String(str(self.__int))
252+
253+
class List:
254+
"列表类型,是对Python原版列表的扩展"
255+
def __init__(self, list_) -> None:
256+
if type(list_) is not list:
257+
raise TypeError(
258+
"请使用列表类型创建List对象"
259+
)
260+
self.__list = list_
261+
self.length = len(list_)
262+
263+
def __repr__(self) -> str:
264+
return "[%s]"%(", ".join(self.__list))
265+
266+
__str__ = __repr__
267+
268+
def __add__(self, other):
269+
"当与一个整数或另一个List对象进行加操作时调用"
270+
if type(other) is List:
271+
return List(self.__list + other.__list)
272+
else:
273+
return List(self.__list + other)
274+
275+
def __sub__(self, other):
276+
"当与一个整数或另一个List对象进行减操作时调用"
277+
if type(other) is List:
278+
return List(self.__list - other.__list)
279+
else:
280+
return List(self.__list - other)
281+
282+
def __eq__(self, other) -> bool:
283+
"等于判断"
284+
if type(other) is List:
285+
return self.__list == other.__list
286+
else:
287+
return self.__list == other
288+
289+
def __ne__(self, other) -> bool:
290+
"不等于判断"
291+
if type(other) is List:
292+
return self.__list != other.__list
293+
else:
294+
return self.__list != other
295+
296+
def __lt__(self, other) -> bool:
297+
"小于判断"
298+
if type(other) is List:
299+
return self.__list < other.__list
300+
else:
301+
return self.__list < other
302+
303+
def __gt__(self, other) -> bool:
304+
"大于判断"
305+
if type(other) is List:
306+
return self.__list > other.__list
307+
else:
308+
return self.__list > other
309+
310+
def __le__(self, other) -> bool:
311+
"小于等于判断"
312+
if type(other) is List:
313+
return self.__list <= other.__list
314+
else:
315+
return self.__list <= other
316+
317+
def __ge__(self, other) -> bool:
318+
"大于等于判断"
319+
if type(other) is List:
320+
return self.__list >= other.__list
321+
else:
322+
return self.__list >= other
323+
324+
def toStrList(self):
325+
"如果列表包含整数,返回一个将整数转为字符串的列表"
326+
strList = []
327+
for i in self.__list:
328+
if i is int:
329+
strList.append(str(i))
330+
else:
331+
strList.append(i)
332+
return strList
333+
334+
def toIntList(self):
335+
"如果列表包含字符串,返回一个将字符串转为整数的列表"
336+
intList = []
337+
for i in self.__list:
338+
if i.isdigit():
339+
intList.append(int(i))
340+
else:
341+
intList.append(i)
342+
return intList
343+
344+
def toStringList(self):
345+
"如果列表包含字符串,返回一个将字符串转为String对象的列表"
346+
stringList = []
347+
for i in self.__list:
348+
if i is str:
349+
stringList.append(String(i))
350+
else:
351+
stringList.append(i)
352+
return stringList
353+
354+
def toIntegerList(self):
355+
"如果列表包含整数,返回一个将整数转为Integer对象的列表"
356+
integerList = []
357+
for i in self.__list:
358+
if i is int:
359+
integerList.append(Integer(i))
360+
else:
361+
integerList.append(i)
362+
return integerList

0 commit comments

Comments
 (0)