鱼C论坛

 找回密码
 立即注册
查看: 2554|回复: 1

[作品展示] 可变字符串

[复制链接]
发表于 2023-8-29 20:45:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. """
  2. Are you still suffering from inefficiency caused by frequent changes to strings?
  3. This module can help you solve that problem!
  4. """
  5. class char(str):
  6.     def __init__(self, string):
  7.         if isinstance(string, str):
  8.             if len(string) == 1:
  9.                 self.char = string
  10.             else:
  11.                 raise ValueError("the length outside 1")
  12.         else:
  13.             raise TypeError("input is a not string")

  14. class MutableString(object):
  15.    
  16.     def __init__(self, string):
  17.         '''
  18.         input: A string object
  19.         output: None
  20.         init a new object
  21.         '''
  22.         if isinstance(string, str):
  23.             self.string = list(string)
  24.         elif isinstance(string, list):
  25.             self.string = string
  26.         else:
  27.             raise TypeError("arg can only be a list or string")
  28.         pass
  29.    
  30.     def __add__(self, other):
  31.         if isinstance(other, MutableString):
  32.             return MutableString(self.string + other.string)
  33.         elif isinstance(other, str):
  34.             return MutableString(self.string + list(other))
  35.         else:
  36.             raise TypeError("arg can only be a MutableString object or a string")
  37.         pass

  38.     __radd__ = __add__

  39.     def __mul__(self, other):
  40.         if isinstance(other, int):
  41.             return MutableString(self.string * other)
  42.    
  43.     def __getattr__(self, name):
  44.         if name == "str":
  45.             return self.get_string()
  46.         else:
  47.             return super().__getattr__(name)
  48.         pass

  49.     def __setattr__(self, name, value):
  50.         if name == "str":
  51.             if isinstance(value, str):
  52.                 self.string = list(value)
  53.             elif isinstance(value, list):
  54.                 self.string = value
  55.             else:
  56.                 raise TypeError("arg can only be a list or string")
  57.         else:
  58.             super().__setattr__(name, value)
  59.         pass

  60.     def __getitem__(self, index):
  61.         return self.string[index]

  62.     def __setitem__(self, index, value):
  63.         self.string[index] = value
  64.         pass

  65.     def __delitem__(self, index):
  66.         del self.string[index]
  67.         pass

  68.     def __repr__(self):
  69.         return """+"".join(self.string)+"""

  70.     __str__ = __repr__

  71.     # new function
  72.    
  73.     def append(self, string: char):
  74.         '''
  75.         input: A string object
  76.         output: None
  77.         append value
  78.         '''
  79.         if isinstance(string, char):
  80.             self.string.extend(list(string))
  81.             pass
  82.         else:
  83.             raise TypeError("arg can only be a char")
  84.         pass
  85.    
  86.     def pop(self, index: int):
  87.         '''
  88.         input: A index value (int type)
  89.         output: a string object
  90.         delete the element corresponding to the index
  91.         '''
  92.         try:
  93.             return self.string.pop(index)
  94.         except IndexError:
  95.             raise IndexError("string index out of range")
  96.         except (ValueError, TypeError):
  97.             raise TypeError("input is a not number")
  98.         pass

  99.     def insert(self, index: int, value: char):
  100.         '''
  101.         input: Insert index, value
  102.         output: None
  103.         insert value in index
  104.         '''
  105.         if isinstance(value, char):
  106.             self.string.insert(index, value)
  107.         else:
  108.             raise TypeError("arg can only be a char")
  109.         pass

  110.     def delete_paragraph(self, start: int=None, stop: int=None):
  111.         '''
  112.         input: Start index, stop index
  113.         output: None
  114.         delete all char in start - stop
  115.         '''
  116.         if start == None:
  117.             start = 0
  118.         if stop == None:
  119.             stop = len(self.string)
  120.         if isinstance(start, int) and isinstance(stop, int):
  121.             del self.string[start:stop]
  122.         pass
  123.    
  124.     def get_string(self):
  125.         '''
  126.         input: string function name, all args (tuple type)
  127.         output: string function return value
  128.         run string function
  129.         '''
  130.         return ''.join(self.string)
复制代码


优点:
进行操作时不用创建一个新字符串。

缺点:
参数必须是char类型。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-9 09:20:44 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 19:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表