在Python中创建自定义字符串类型

Yot*_*nin 6 python string unicode class

在Python中,是否有创建自定义字符串类的选项,可以通过键入以下内容来创建:

a = b"some string"
a.someCustomMethod()
Run Code Online (Sandbox Code Playgroud)

就像python有它u""r""字符串一样?

use*_*ica 6

编写自己的字符串类很简单,但是无法获得所需的构造语法.你能得到的最接近的是

a = MyString("some string")
Run Code Online (Sandbox Code Playgroud)

MyString你的自定义类在哪里.我想b = MyString如果你愿意,你可以别名.

另请注意,这b"some string"已经是bytestring文字语法.在Python 2中,它只是一个常规字符串.在Python 3中,它创建了一个bytes对象,因为常规字符串在Python 3中是unicode.