Skip to main content

saferepr

Safe version of :func:repr.

def saferepr(
o: Any,
maxlen: int = None,
maxlevels: int = 3,
seen: Set = None
) - > string

Safe version of :func:repr. Warning: Make sure you set the maxlen argument, or it will be very slow for recursive objects. With the maxlen set, it's often faster than built-in repr.

Parameters​

NameTypeDescription
oAnyThe object to be converted into a string representation.
maxlenint = NoneThe maximum number of characters allowed in the resulting string; essential for performance with recursive objects.
maxlevelsint = 3The maximum depth of nested structures to traverse before truncating the output.
seenSet = NoneA set of object IDs already processed to prevent infinite recursion in cyclic data structures.

Returns​

TypeDescription
stringA string representation of the object, truncated if it exceeds the specified maximum length or recursion depth.