|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- """
- Are you still suffering from inefficiency caused by frequent changes to strings?
- This module can help you solve that problem!
- """
- class char(str):
- def __init__(self, string):
- if isinstance(string, str):
- if len(string) == 1:
- self.char = string
- else:
- raise ValueError("the length outside 1")
- else:
- raise TypeError("input is a not string")
- class MutableString(object):
-
- def __init__(self, string):
- '''
- input: A string object
- output: None
- init a new object
- '''
- if isinstance(string, str):
- self.string = list(string)
- elif isinstance(string, list):
- self.string = string
- else:
- raise TypeError("arg can only be a list or string")
- pass
-
- def __add__(self, other):
- if isinstance(other, MutableString):
- return MutableString(self.string + other.string)
- elif isinstance(other, str):
- return MutableString(self.string + list(other))
- else:
- raise TypeError("arg can only be a MutableString object or a string")
- pass
- __radd__ = __add__
- def __mul__(self, other):
- if isinstance(other, int):
- return MutableString(self.string * other)
-
- def __getattr__(self, name):
- if name == "str":
- return self.get_string()
- else:
- return super().__getattr__(name)
- pass
- def __setattr__(self, name, value):
- if name == "str":
- if isinstance(value, str):
- self.string = list(value)
- elif isinstance(value, list):
- self.string = value
- else:
- raise TypeError("arg can only be a list or string")
- else:
- super().__setattr__(name, value)
- pass
- def __getitem__(self, index):
- return self.string[index]
- def __setitem__(self, index, value):
- self.string[index] = value
- pass
- def __delitem__(self, index):
- del self.string[index]
- pass
- def __repr__(self):
- return """+"".join(self.string)+"""
- __str__ = __repr__
- # new function
-
- def append(self, string: char):
- '''
- input: A string object
- output: None
- append value
- '''
- if isinstance(string, char):
- self.string.extend(list(string))
- pass
- else:
- raise TypeError("arg can only be a char")
- pass
-
- def pop(self, index: int):
- '''
- input: A index value (int type)
- output: a string object
- delete the element corresponding to the index
- '''
- try:
- return self.string.pop(index)
- except IndexError:
- raise IndexError("string index out of range")
- except (ValueError, TypeError):
- raise TypeError("input is a not number")
- pass
- def insert(self, index: int, value: char):
- '''
- input: Insert index, value
- output: None
- insert value in index
- '''
- if isinstance(value, char):
- self.string.insert(index, value)
- else:
- raise TypeError("arg can only be a char")
- pass
- def delete_paragraph(self, start: int=None, stop: int=None):
- '''
- input: Start index, stop index
- output: None
- delete all char in start - stop
- '''
- if start == None:
- start = 0
- if stop == None:
- stop = len(self.string)
- if isinstance(start, int) and isinstance(stop, int):
- del self.string[start:stop]
- pass
-
- def get_string(self):
- '''
- input: string function name, all args (tuple type)
- output: string function return value
- run string function
- '''
- return ''.join(self.string)
复制代码
优点:
进行操作时不用创建一个新字符串。
缺点:
参数必须是char类型。 |
|