我有一个字典对象的包装类,我很好奇,如果在我的课程中实现__repr__
方法时,一个人比另一个更受欢迎:
class MyClass(object):
def __init__(self):
self._obj = dict()
def __repr__(self):
return self._obj.__str__()
OR
class MyClass(object):
def __init__(self):
self._obj = dict()
def __repr__(self):
return str(self._obj)
str()
的第二个实现对我来说是对的。我只是想确保我没有遗漏任何东西。
答案 0 :(得分:6)
你是对的。你应该避免直接调用双下划线方法:内置函数是Pythonic的方法。
(例如df <- structure(list(TIME = structure(c(11164, 11296, 11296, 11296,
11330, 11330, 11547, 11547, 11547, 11547, 11639, 11639, 11639,
11639, 11758, 11758, 11758, 11758, 11869, 11869, 11869, 11869,
11968, 11968, 11968, 11968, 12009, 12009, 12009, 12102, 12102,
12102, 12102, 12102, 12193, 12193, 12193, 12193, 12193, 12193,
12193, 12282, 12282, 12282, 12282, 12282, 12282, 12368, 12368,
12368, 12368, 12500, 12500, 12500, 12500, 12500, 12500, 12500,
12590, 12590, 12590, 12590, 12590, 12590, 12590, 12684, 12684,
12684, 12684, 12684, 12800, 12800, 12800, 12800, 12800, 12891,
12891, 12891, 12891, 12891, 12891, 12891, 12998, 12998, 12998,
12998, 13101, 13101, 13101, 13101, 13101, 13273, 13273, 13273,
13273, 13362, 13362, 13362, 13362, 13459, 13459, 13459, 13459,
13459, 13459, 13557, 13557, 13557, 13557, 13557, 13557, 13557,
13654, 13654, 13654, 13654, 13654, 13654, 13767, 13767, 13767,
13767, 13767, 13767, 13767, 13857, 13857, 13857, 13857, 13857,
13857, 13857, 13949, 13949, 13949, 13949, 13949, 13949, 13949,
14048, 14048, 14048, 14048, 14048, 14048, 14048, 14398, 14398,
14398, 14398, 14398, 14755, 14755, 14755, 14755, 14755, 14755,
14755, 14943, 14943, 14943, 14943, 14943, 14943, 15806, 15806,
15806, 15806, 15806, 15806, 15806, 16135, 16135, 16135, 16135,
16135, 16135), class = "Date"), GAUGE_ID = c(198L, 200L, 199L,
198L, 202L, 198L, 204L, 198L, 203L, 201L, 203L, 204L, 201L, 198L,
201L, 198L, 203L, 204L, 203L, 201L, 204L, 198L, 203L, 204L, 201L,
198L, 199L, 202L, 200L, 203L, 201L, 204L, 198L, 199L, 203L, 201L,
202L, 204L, 198L, 200L, 199L, 201L, 200L, 199L, 198L, 203L, 204L,
198L, 201L, 203L, 204L, 203L, 204L, 202L, 201L, 200L, 199L, 198L,
203L, 204L, 202L, 198L, 199L, 200L, 201L, 198L, 199L, 201L, 203L,
204L, 199L, 198L, 201L, 203L, 204L, 203L, 200L, 199L, 198L, 204L,
202L, 201L, 198L, 203L, 201L, 204L, 199L, 198L, 201L, 203L, 204L,
201L, 198L, 203L, 204L, 201L, 198L, 203L, 204L, 198L, 199L, 201L,
203L, 204L, 200L, 198L, 199L, 201L, 202L, 203L, 204L, 200L, 204L,
200L, 203L, 201L, 199L, 198L, 204L, 202L, 201L, 199L, 198L, 203L,
200L, 201L, 202L, 203L, 204L, 200L, 199L, 198L, 200L, 204L, 203L,
202L, 201L, 199L, 198L, 204L, 203L, 202L, 201L, 199L, 200L, 198L,
199L, 201L, 204L, 203L, 202L, 200L, 203L, 202L, 201L, 199L, 204L,
198L, 204L, 203L, 202L, 201L, 200L, 198L, 198L, 199L, 201L, 200L,
203L, 204L, 202L, 198L, 199L, 200L, 202L, 204L, 201L), PH = c(7.22,
7.2, 6.9, 7.11, 7.01, 7.03, 7.03, 7.17, 6.99, 6.99, 6.85, 6.84,
6.88, 6.95, 6.94, 7.04, 6.93, 6.93, 7.21, 7.23, 7.19, 7.25, 7.07,
7.09, 7.09, 7.16, 7.43, 7.47, 7.41, 7.08, 7.05, 7.07, 7.1, 7.15,
7.12, 7.16, 7.25, 7.2, 7.26, 7.49, 7.62, 7.12, 7.16, 7.44, 7.3,
7.11, 7.2, 7.21, 7.14, 7.06, 6.66, 7.28, 7.29, 7.42, 7.22, 7.63,
7.52, 7.35, 7.05, 7.23, 7.16, 7.32, 7.3, 7.22, 7.19, 7.34, 7.55,
7.27, 7.26, 7.36, 7.42, 7.35, 7.2, 7.3, 7.24, 7.19, 7.45, 7.5,
7.36, 7.21, 7.34, 7.25, 7.11, 7.01, 7.03, 6.95, 7.36, 7.3, 7.2,
7.21, 7.2, 7.21, 7.23, 7.16, 7.12, 7.19, 7.29, 7.19, 7.14, 5.31,
6.99, 7.15, 7.16, 6.87, 7.12, 7.3, 7.67, 7.28, 7.54, 7.29, 7.45,
7.44, 7.17, 7.13, 7.13, 7.14, 7.68, 7.6, 6.89, 7.42, 7.3, 7.4,
7.28, 7.29, 7.23, 7.21, 7.44, 7.21, 7.4, 7.26, 7.48, 7.29, 7.33,
7.08, 7.27, 7.57, 7.28, 7.69, 7.44, 7.31, 7.36, 7.4, 7.36, 7.47,
7.75, 7.5, 6.97, 7.08, 6.96, 7.1, 7.07, 7.13, 7.38, 7.11, 7.22,
7.19, 7.13, 7.29, 7.12, 7.03, 7.09, 7.23, 7, 7.11, 7.85, 7.8,
7.36, 8.01, 7.03, 7.33, 7.32, 7.31, 7.56, 7.27, 7.32, 7.08, 7.07
), EH = c(100L, 470L, 462L, 1L, 427L, -6L, 118L, -24L, 57L, -12L,
93L, 136L, 119L, 101L, 128L, 32L, 35L, 137L, -4L, 107L, 84L,
79L, -1L, 71L, 108L, 5L, 467L, 450L, 470L, 11L, 111L, 80L, -2L,
160L, -16L, 148L, 215L, 103L, -45L, 260L, 300L, 141L, 288L, 335L,
64L, 85L, 120L, 86L, 129L, 72L, 144L, 49L, 108L, 335L, 170L,
285L, 308L, 5L, 49L, 108L, 335L, 5L, 308L, 285L, 170L, 78L, 282L,
145L, 70L, 146L, 325L, 92L, 137L, 94L, 137L, 51L, 283L, 290L,
77L, 116L, 287L, 143L, 87L, 81L, 129L, 117L, 243L, 29L, 114L,
-5L, 105L, 131L, 121L, 60L, 88L, 127L, 69L, 62L, 122L, 64L, 450L,
128L, 45L, 117L, 340L, 287L, 436L, 165L, 406L, 122L, 175L, 441L,
136L, 325L, 81L, 148L, 285L, 118L, 209L, 186L, 204L, 433L, 176L,
153L, 420L, 182L, 380L, 150L, 195L, 379L, 455L, 143L, 391L, 379L,
178L, 339L, 165L, 435L, 125L, 161L, 128L, 416L, 173L, 381L, 420L,
130L, 333L, 153L, 151L, 79L, 246L, 428L, 147L, 330L, 161L, 217L,
155L, 75L, 139L, 104L, 164L, 167L, 302L, 69L, 223L, 226L, 206L,
331L, 330L, 485L, 237L, 111L, 291L, 386L, 64L, 155L, 170L)), .Names = c("TIME",
"GAUGE_ID", "PH", "EH"), class = "data.frame", row.names = c(NA,
-177L))
与len(foo)
相同。)
答案 1 :(得分:1)
问问自己:如果outputNode(Node node, int level)
{
// here, compute indentation based on level.
// you'll want to put that indentation before every output line
writeLine("{");
writeLine("name:", node.Name);
writeLine("children: [");
for each child in children
outputNode(child, level+1);
writeLine("]");
writeLine("}");
}
可用,为什么人们会使用__str__
?
str
只是您想要修改默认__str__
方法时可以使用的重载。同样适用于str
。
你会用这个吗?
__len__
答案理念:向提问者展示核心理解,最好让提问者自己得出正确答案。