Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 利用Python3中的集合模块_Python 3.x_Collections - Fatal编程技术网

Python 3.x 利用Python3中的集合模块

Python 3.x 利用Python3中的集合模块,python-3.x,collections,Python 3.x,Collections,相对尖锐的问题。目前正在运行Python3.4.1,我正在做一个面向对象的练习,我需要覆盖继承类中的一些函数 目标: 从内置模块集合导入并使用collections.UserList重写追加、扩展,以便在检测到重复项时不会“添加”任何重复项。(本部分已完成) 问题: 主要的问题是,我仍然在学习面向对象编程,我想构建可以轻松键入和返回的对象,因此我正在为我的类编写一个str和repr 目前我的课程如下所示:(省略了“目标”部分,因为它有效) 然后,我决定运行一些示例代码,以便更好地度量: >

相对尖锐的问题。目前正在运行Python3.4.1,我正在做一个面向对象的练习,我需要覆盖继承类中的一些函数

目标: 从内置模块集合导入并使用collections.UserList重写追加、扩展,以便在检测到重复项时不会“添加”任何重复项。(本部分已完成)

问题: 主要的问题是,我仍然在学习面向对象编程,我想构建可以轻松键入和返回的对象,因此我正在为我的类编写一个strrepr

目前我的课程如下所示:(省略了“目标”部分,因为它有效)

然后,我决定运行一些示例代码,以便更好地度量:

>>> x = UList ([4,5,6])
>>> x.entry
[4, 5, 6]
>>> x
[4, 5, 6]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    x
TypeError: __repr__ returned non-string (type NoneType)
>>> print(x)
[4, 5, 6]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print(x)
TypeError: __str__ returned non-string (type NoneType)
>x=UList([4,5,6])
>>>十、进入
[4, 5, 6]
>>>x
[4, 5, 6]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
x
TypeError:\uuuuuu repr\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
>>>打印(x)
[4, 5, 6]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
打印(x)
TypeError:\uuuuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
通常我会直视对象,试图找出哪里出了问题,但我有点困惑,因为我还是新的=(.有人能帮我解释一下为什么即使在我覆盖了init之后它仍返回一个NoneType吗?(另外,一个可能的解决方案是如何纠正错误,这样就不会有任何错误了)

考虑一下(注意,
\uuu str\uuuu
末尾没有明确的
返回:

问题是,
\uuuuuu repr\uuuuuu
\uuuu str\uuuuuuu
需要返回一个字符串。如果可能,从
\uuuuuuu repr\uuuuu
返回的字符串应该是对象的“官方”字符串表示形式,以便
eval
根据


除了Python表达式之外,还可以使用更方便或简洁的表示形式上的文档。

您的
\uu str\uuu
方法需要返回一个字符串。现在,它返回
None
我很高兴您的问题得到了解决!但是,请不要将修复编辑到问题本身中——这会让您很难说出问题的内容问题最初是,这使得其他用户更难从中学习。接受答案已经表明您的问题已成功解决!
>>> x = UList ([4,5,6])
>>> x.entry
[4, 5, 6]
>>> x
[4, 5, 6]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    x
TypeError: __repr__ returned non-string (type NoneType)
>>> print(x)
[4, 5, 6]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print(x)
TypeError: __str__ returned non-string (type NoneType)
>>> class Foo:
...    def __str__(self):
...       print('Foo!!')
... 
>>> f=Foo()
>>> f
<__main__.Foo object at 0x10a655080>
>>> print(f)
Foo!!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __str__ returned non-string (type NoneType)
>>> class Foo:
...    def __str__(self):
...       return 'Foo!!!'
... 
>>> f=Foo()
>>> print(f)
Foo!!!