Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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-从float继承并调用str和repr时发生递归错误_Python_String_Inheritance_Repr - Fatal编程技术网

Python-从float继承并调用str和repr时发生递归错误

Python-从float继承并调用str和repr时发生递归错误,python,string,inheritance,repr,Python,String,Inheritance,Repr,为了好玩,我在Python中测试了一些特性; 但我有一个递归错误,我不明白 类Testfloat: 定义新cls,值: 返回super.\uuuu new\uuuu cls,值 定义自身: 返回超级__ 定义报告自我: 返回f test=Test12 测验 返回super.\uuuu str\uuuuu应该调用float.\uuuuu str\uuuuuu,然后只返回'12' 你有什么想法吗?你的“repr”调用你的“str”,它调用超级的“str”,它遵从repr,它调用你的“repr”,这是

为了好玩,我在Python中测试了一些特性; 但我有一个递归错误,我不明白

类Testfloat: 定义新cls,值: 返回super.\uuuu new\uuuu cls,值 定义自身: 返回超级__ 定义报告自我: 返回f test=Test12 测验 返回super.\uuuu str\uuuuu应该调用float.\uuuuu str\uuuuuu,然后只返回'12'

你有什么想法吗?

你的“repr”调用你的“str”,它调用超级的“str”,它遵从repr,它调用你的“repr”,这是一个无限递归。你可以在你的方法中调用super.\uuuu repr\uu,而不是调用strself

核心问题是浮动。斯特尔夫将称为赛尔夫。而不是浮动。赛尔夫

这不仅意味着你有一个从测试到测试的无限递归。从测试到浮点数。从测试到浮点数。从测试到重复,这意味着测试。从测试到打印和测试一样的东西。我想你不会想要它,因为你努力重新实现它

相反,我认为你想要:

class Test(float):
    def __str__(self):
        return super().__repr__()
    
    def __repr__(self):
        return f'<value: {super().__repr__()}>'

请发布完整的错误回溯!您的repr调用str,str调用float的str,str遵从repr,repr是一个无限递归。你可以调用super.\uuuurepr\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu__
>>> Test(12)
<value: 12.0>
class Test(float):
    def __str__(self):
        return super().__repr__()
    
    def __repr__(self):
        return f'<value: {super().__repr__()}>'