在没有 str 方法的情况下从类中检索数据 [英] Retrieving data from a class without the str method
问题描述
我拥有一个名为 Collatz
的类和一个创建类对象的函数 collatz_it
,我正在尝试生成一个数字要达到的步骤数1 使用 collatz conjucture 及其相应的步骤,直到 100 万使用生成器
I posses a class called Collatz
and a function collatz_it
which creates an object of the class, I'm trying to generate the number of steps for a number to reach 1 using the collatz conjucture and their corresponding steps till 1 million using a generator
import collatz
values = {}
count = 0
#collatz.collatz_it(n)
def gen():
n = 0
x = 0
while True:
yield x
n += 1
x = collatz.collatz_it(n)
for i in gen():
count += 1
values[count] = i
print values
if count == 1000000:
break
如您所见,我使用collatz猜想为给定的数字生成达到1所需的步数,并将其添加到具有相应数字的字典中但是当我打印出来时字典值,它的输出很尴尬:
As you can see, I generate the amount of steps taken for it to reach 1 using the collatz conjecture for a number given and add it to a dictionary with the corresponding number but when I print out the dictionary values, it's output is awkwardly something like this:
{1: 0}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>, 5: <collatz.Collatz instance at 0x01DCDEB8>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>, 5: <collatz.Collatz instance at 0x01DCDEB8>, 6: <collatz.Collatz instance at 0x01DCDE90>}
{1: 0, 2: <collatz.Collatz instance at 0x01DCA580>, 3: <collatz.Collatz instance at 0x01DCDF58>, 4: <collatz.Collatz instance at 0x01DCDFA8>, 5: <collatz.Collatz instance at 0x01DCDEB8>, 6: <collatz.Collatz instance at 0x01DCDE90>, 7: <collatz.Collatz instance at 0x01DE8940>}
如果我打印 print i
而不是 print values
我得到所需的输出,这基本上是因为 print
语句触发了 __str__
类中的方法
If I print print i
instead of print values
I get the required output, this is basically because the print
statement triggers the __str__
method in the class
是否有任何方法可以在不输入 <collatz.Collatz 实例 at 0x01DCDFA8>
的情况下将实际步骤添加到字典中,是否有任何方法可以从 <collatz.Collatz 实例中检索数据?code>__str__
方法使我的字典看起来像这样:
Isn't there any way I could add the actual steps to the dictionary without entering <collatz.Collatz instance at 0x01DCDFA8>
, Is there any sort of method of retrieving data as from a __str__
method so that my dictionary looks something like this:
{1: 0}
{1: 0, 2: 1}
{1: 0, 2: 1, 3: 7}
推荐答案
任何 Python 容器的默认表示是使用内容的 repr()
输出,而不是 str()代码>.
The default representation of any Python container is to use the repr()
output of the contents, not str()
.
解决方案是给 collatz.Collatz()
实例一个 __repr__
方法(你可以用猴子补丁),或者使用一个dict
的子类,在显示内容时使用 str()
而不是 repr()
.
The solution would be for you to either give the collatz.Collatz()
instance a __repr__
method (you can monkey-patch that in), or to use a subclass of dict
that uses str()
instead of repr()
when displaying the contents.
__repr__
中的猴子补丁可以很简单:
Monkey-patching in a __repr__
could be as simple as:
collatz.Collatz.__repr__ = collatz.Collatz.__str__
当然,如果这是您自己的代码,只需在类主体中定义一个__repr__
方法即可.
Of course, if this is your own code, just define a __repr__
method in the class body itself.
这篇关于在没有 str 方法的情况下从类中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!