2
2
对Python原版类型的扩展
3
3
"""
4
4
__author__ = "Jerry"
5
- __version__ = "1.3.3 "
5
+ __version__ = "1.3.4 "
6
6
7
7
__all__ = ["String" , "Integer" , "List" ]
8
8
@@ -11,7 +11,7 @@ class NotComposedOfNumbersError(Exception):
11
11
pass
12
12
13
13
class String (str ):
14
- "字符串类,是对Python原版字符串的加强 "
14
+ "字符串类,是对Python原版字符串的扩展 "
15
15
def __init__ (self , str_ ) -> None :
16
16
if type (str_ ) is not str :
17
17
raise TypeError (
@@ -20,11 +20,6 @@ def __init__(self, str_) -> None:
20
20
self .__str = str_
21
21
self .length = len (str_ ) # 字符串大小
22
22
23
- def __repr__ (self ) -> str :
24
- return self .__str
25
-
26
- __str__ = __repr__
27
-
28
23
def __len__ (self ):
29
24
"使用len()函数时调用"
30
25
return self .length
@@ -89,15 +84,6 @@ def getNormal(self):
89
84
"将扩展类型转为原版类型"
90
85
return self .__str
91
86
92
- def isNumber (self ):
93
- "判断字符串是否由数字组成,返回布尔值"
94
- try :
95
- int (self .__str )
96
- except ValueError :
97
- return False
98
- else :
99
- return True
100
-
101
87
def isEven (self ):
102
88
"判断数字是否是偶数,返回布尔值(前提是这个字符串是由数字组成的,否则抛出NotComposedOfNumbersError异常)"
103
89
if self .toInt () % 2 == 0 :
@@ -152,19 +138,14 @@ def toInteger(self):
152
138
) from e
153
139
154
140
class Integer (int ):
155
- "整数类型,是对Python原版整数的加强 "
141
+ "整数类型,是对Python原版整数的扩展 "
156
142
def __init__ (self , int_ ) -> None :
157
143
if type (int_ ) is not int :
158
144
raise TypeError (
159
145
"请使用整数类型创建Integer对象"
160
146
)
161
147
self .__int = int_
162
148
163
- def __repr__ (self ) -> str :
164
- return self .toStr ()
165
-
166
- __str__ = __repr__
167
-
168
149
def __add__ (self , other ):
169
150
"当与一个整数或另一个Integer对象进行加操作时调用"
170
151
if type (other ) is Integer :
@@ -259,7 +240,7 @@ def toString(self):
259
240
return String (str (self .__int ))
260
241
261
242
class List (list ):
262
- "列表类型,是对Python原版列表的加强 "
243
+ "列表类型,是对Python原版列表的扩展 "
263
244
def __init__ (self , list_ ) -> None :
264
245
if type (list_ ) is not list :
265
246
raise TypeError (
@@ -388,4 +369,4 @@ def toIntegerList(self):
388
369
integerList .append (Integer (i ))
389
370
else :
390
371
integerList .append (i )
391
- return List (integerList )
372
+ return List (integerList )
0 commit comments