原文:https://docs.scipy.org/doc/numpy/reference/generated/numpy.set_string_function.html
校对:(虚位以待)
numpy.set_string_function(f, repr=True)[source]设置当漂亮打印数组时使用的Python函数。
| 参数: | f:function或None 
 repr:bool,可选 
 | 
|---|
例子
>>> def pprint(arr):
...     return 'HA! - What are you going to do now?'
...
>>> np.set_string_function(pprint)
>>> a = np.arange(10)
>>> a
HA! - What are you going to do now?
>>> print(a)
[0 1 2 3 4 5 6 7 8 9]
我们可以将该函数重置为默认值:
>>> np.set_string_function(None)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
repr会影响漂亮的打印或正常的字符串表示。请注意,__repr__仍然受设置__str__的影响,因为返回的字符串中每个数组元素的宽度等于__str__()
>>> x = np.arange(4)
>>> np.set_string_function(lambda x:'random', repr=False)
>>> x.__str__()
'random'
>>> x.__repr__()
'array([     0,      1,      2,      3])'