![]() |
#2
fall_bernana2019-11-07 10:02
回复 楼主 glaciya
![]() >>> class A(object): ... def __str__(self): ... return "this is __str__" ... def __repr__(self): ... return "this is __repr__" ... >>> a = A() >>> a this is __repr__ >>> print(a) this is __str__ >>> ![]() object.__str__(self) Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used. The default implementation defined by the built-in type object calls object.__repr__(). object.__repr__(self) Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required. ![]() >>> help(Counter.__repr__) Help on function __repr__ in module collections: __repr__(self) Return repr(self). >>> help(Counter.__str__) Help on wrapper_descriptor: __str__(self, /) Return str(self). repr(object) 返回包含一个对象的可打印表示形式的字符串。 对于许多类型来说,该函数会尝试返回的字符串将会与该对象被传递给 eval() 时所生成的对象具有相同的值,在其他情况下表示形式会是一个括在尖括号中的字符串,其中包含对象类型的名称与通常包括对象名称和地址的附加信息。 类可以通过定义 __repr__() 方法来控制此函数为它的实例所返回的内容。 我的理解是Counter并没有实现这个功能而list实现了.如果你想可以自己修改Counter [此贴子已经被作者于2019-11-7 10:08编辑过] |
最近在学python 发现一个问题。当使用counter功能时,感觉是得到了
一个对象地址,但是不明白为什么不能直接输出地址里的数据,反而是需要结合List才能看到里面的数据,请问这是什么原因?是否相当于java里的需要使用某种方法(将对象作为参数)才能显示对象的信息。
In [42]: from collections import Counter
In [43]: a=Counter(wordsplits)
In [44]: print(a)
Counter({'a': 4, 'long': 2, 'time': 2, 'ago,': 2, 'there': 2, 'was': 2, 'prince' : 2, 'and': 2, 'princess': 2, 'living': 2, 'in': 2, 'different': 2, 'casles': 2} )
In [45]: print(a.elements)
<bound method Counter.elements of Counter({'a': 4, 'long': 2, 'time': 2, 'ago,': 2, 'there': 2, 'was': 2, 'prince': 2, 'and': 2, 'princess': 2, 'living': 2, 'in ': 2, 'different': 2, 'casles': 2})>
In [46]: print(a.elements())
<itertools.chain object at 0x7f37ea7430f0>
In [47]: a.elements()
Out[47]: <itertools.chain at 0x7f37ea7430b8>
In [48]: list(a.elements())
Out[48]:
['long',
'long',
'time',
'time',
'ago,',
'ago,',
'there',
'there',
'was',
'was',
'a',
'a',
'a',
'a',
'prince',
'prince',
'and',
'and',
'princess',
'princess',
'living',
'living',
'in',
'in',
'different',
'different',
'casles',
'casles']
问题2:
为什么使用counter之后的对象不能直接输出,使用print打印出来的是个地址。
In [62]: for numvalue in a:
...: print(numvalue,a[numvalue], end=' ')
...:
...:
...:
...:
long 2 time 2 ago, 2 there 2 was 2 a 4 prince 2 and 2 princess 2 living 2 in 2 different 2 casles 2
In [63]: copyword={}
In [64]: for numvalue in a:
...: print(numvalue,a[numvalue], end=' ')
...: copyword[numvalue]=a[numvalue]
...: print(copyword)
...:
...:
...:
...:
long 2 time 2 ago, 2 there 2 was 2 a 4 prince 2 and 2 princess 2 living 2 in 2 different 2 casles 2
{'long': 2, 'time': 2, 'ago,': 2, 'there': 2, 'was': 2, 'a': 4, 'prince': 2, 'and': 2, 'princess': 2, 'living': 2, 'in': 2, 'different': 2, 'casles': 2}
In [65]: copyword
Out[65]:
{'long': 2,
'time': 2,
'ago,': 2,
'there': 2,
'was': 2,
'a': 4,
'prince': 2,
'and': 2,
'princess': 2,
'living': 2,
'in': 2,
'different': 2,
'casles': 2}