numpy 1.22.4 Pypi GitHub Homepage
Other Docs
Parameters
set_string_function(f, repr=True)

Parameters

f : function or None

Function to be used to pretty print arrays. The function should expect a single array argument and return a string of the representation of the array. If None, the function is reset to the default NumPy function to print arrays.

repr : bool, optional

If True (default), the function for pretty printing ( __repr__ ) is set, if False the function that returns the default string representation ( __str__ ) is set.

Set a Python function to be used when pretty printing arrays.

See Also

get_printoptions
set_printoptions

Examples

>>> 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?
>>> _ = a
... # [0 1 2 3 4 5 6 7 8 9]

We can reset the function to the default:

>>> np.set_string_function(None)
... a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

:None:None:`repr` affects either pretty printing or normal string representation. Note that __repr__ is still affected by setting __str__ because the width of each array element in the returned string becomes equal to the length of the result of __str__() .

>>> x = np.arange(4)
... np.set_string_function(lambda x:'random', repr=False)
... x.__str__() 'random'
>>> x.__repr__()
'array([0, 1, 2, 3])'
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/core/arrayprint.py#1648
type: <class 'function'>
Commit: