MCPcopy Create free account
hub / github.com/numpy/numpy / set_string_function

Function set_string_function

numpy/core/arrayprint.py:1668–1725  ·  view source on GitHub ↗

Set a Python function to be used when pretty printing arrays. 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

(f, repr=True)

Source from the content-addressed store, hash-verified

1666
1667
1668def set_string_function(f, repr=True):
1669 """
1670 Set a Python function to be used when pretty printing arrays.
1671
1672 Parameters
1673 ----------
1674 f : function or None
1675 Function to be used to pretty print arrays. The function should expect
1676 a single array argument and return a string of the representation of
1677 the array. If None, the function is reset to the default NumPy function
1678 to print arrays.
1679 repr : bool, optional
1680 If True (default), the function for pretty printing (``__repr__``)
1681 is set, if False the function that returns the default string
1682 representation (``__str__``) is set.
1683
1684 See Also
1685 --------
1686 set_printoptions, get_printoptions
1687
1688 Examples
1689 --------
1690 >>> def pprint(arr):
1691 ... return 'HA! - What are you going to do now?'
1692 ...
1693 >>> np.set_string_function(pprint)
1694 >>> a = np.arange(10)
1695 >>> a
1696 HA! - What are you going to do now?
1697 >>> _ = a
1698 >>> # [0 1 2 3 4 5 6 7 8 9]
1699
1700 We can reset the function to the default:
1701
1702 >>> np.set_string_function(None)
1703 >>> a
1704 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1705
1706 `repr` affects either pretty printing or normal string representation.
1707 Note that ``__repr__`` is still affected by setting ``__str__``
1708 because the width of each array element in the returned string becomes
1709 equal to the length of the result of ``__str__()``.
1710
1711 >>> x = np.arange(4)
1712 >>> np.set_string_function(lambda x:'random', repr=False)
1713 >>> x.__str__()
1714 'random'
1715 >>> x.__repr__()
1716 'array([0, 1, 2, 3])'
1717
1718 """
1719 if f is None:
1720 if repr:
1721 return multiarray.set_string_function(_default_array_repr, 1)
1722 else:
1723 return multiarray.set_string_function(_default_array_str, 0)
1724 else:
1725 return multiarray.set_string_function(f, repr)

Callers 1

inject_strMethod · 0.85

Calls

no outgoing calls

Tested by 1

inject_strMethod · 0.68