Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的“减法”字符串、类_Python_Class - Fatal编程技术网

python中的“减法”字符串、类

python中的“减法”字符串、类,python,class,Python,Class,学习python中的类。我想要两个字符串之间的差,一种减法。例如: a = "abcdef" b ="abcde" c = a - b 这将给出输出f 我在看这门课,我是新来的,所以我想澄清一下它是如何工作的 class MyStr(str): def __init__(self, val): return str.__init__(self, val) def __sub__(self, other): if self.count(other)

学习python中的类。我想要两个字符串之间的差,一种减法。例如:

a = "abcdef"
b ="abcde"
c = a - b
这将给出输出f

我在看这门课,我是新来的,所以我想澄清一下它是如何工作的

class MyStr(str):
    def __init__(self, val):
        return str.__init__(self, val)
    def __sub__(self, other):
        if self.count(other) > 0:
            return self.replace(other, '', 1)
        else:
            return self
这将通过以下方式起作用:

>>> a = MyStr('thethethethethe')
>>> b = a - 'the'
>>> a
'thethethethethe'
>>> b
'thethethethe'
>>> b = a - 2 * 'the'
>>> b
'thethethe'
因此,一个字符串被传递给类,构造函数被称为uu init_uu。这将运行构造函数并返回一个对象,其中包含字符串的值?然后创建一个新的减法函数,这样当您使用MyStr对象时,它只是定义减法如何与该类一起工作?使用字符串调用sub时,count用于检查该字符串是否为所创建对象的子字符串。如果是这种情况,则删除传递的字符串的第一个匹配项。这种理解正确吗

编辑:基本上这个类可以减少到:

class MyStr(str):
    def __sub__(self, other):
            return self.replace(other, '', 1)

是的,你的理解完全正确

如果在左侧操作数上存在,Python将调用;如果不是,则右侧操作数上相应的

请参阅以获取Python支持的钩子列表,以提供更多算术运算符

请注意,.count调用是冗余的。如果其他字符串不存在,则替换不会失败;整个功能可简化为:

def __sub__(self, other):
    return self.replace(other, '', 1)
相反的版本是:

def __rsub__(self, other):
    return other.replace(self, '', 1)

是的,你的理解完全正确

如果在左侧操作数上存在,Python将调用;如果不是,则右侧操作数上相应的

请参阅以获取Python支持的钩子列表,以提供更多算术运算符

请注意,.count调用是冗余的。如果其他字符串不存在,则替换不会失败;整个功能可简化为:

def __sub__(self, other):
    return self.replace(other, '', 1)
相反的版本是:

def __rsub__(self, other):
    return other.replace(self, '', 1)

OP需要注意的一点是:整个_usub_uu函数应该是:return self.replaceother,1Thanks for the response,是rsub-like-sub,但从右边工作还是从右边工作?如果我尝试rsub,那么“-”不起作用,因为我假设它没有挂接在一起工作,而不使用它?很酷,现在我的作品中的类被编辑为三行。为你添加了一个反向版本;相反,原始表达式是other-self,因此您必须相应地调整代码。试着用“thethethe”-MyStr“the”来看看它的作用。@MartijnPieters啊,太好了,现在明白你的意思了。谢谢OP需要注意的一点是:整个_usub_uu函数应该是:return self.replaceother,1Thanks for the response,是rsub-like-sub,但从右边工作还是从右边工作?如果我尝试rsub,那么“-”不起作用,因为我假设它没有挂接在一起工作,而不使用它?很酷,现在我的作品中的类被编辑为三行。为你添加了一个反向版本;相反,原始表达式是other-self,因此您必须相应地调整代码。试着用“thethethe”-MyStr“the”来看看它的作用。@MartijnPieters啊,太好了,现在明白你的意思了。谢谢注意:如果您除了调用超类之外没有做任何其他事情,那么您不需要定义_init _uu。您的理解是正确的,但是请注意,a-the*2不会给出与a-the-the相同的结果。事实上,第二个将产生一个错误。@RoadieRich为什么会产生错误?@Paul a-the返回self.replace…(str)的结果。然后您尝试从str中减去第二个,这是一个无效的操作。@RoadieRich啊,我明白了,谢谢你注意:如果你除了调用超类之外什么都不做,你不需要定义_uinit。你的理解是正确的,但是请注意,a-the*2不会给出与a-the-the-the相同的结果。事实上,第二个将产生一个错误。@RoadieRich为什么会产生错误?@Paul a-the返回self.replace…(str)的结果。然后您试图从str中减去第二个,这是一个无效的操作。@RoadieRich啊,我明白了,谢谢