# Python Alternate repr() implementation

PythonProgrammingServer Side Programming

In Python, if we want to limit the large amount of data from displaying, we can use the reprlib module.

To use this module, we should import it using.

import reprlib


There are different classes and methods related to reprlib. These are −

## Class reprlib.Repr

The Repr class provides formatting services. It creates functions like built-in repr(). In this class we can add size limits and different object types.

## Method reprlib.repr(object)

This method is used to return string like built-in repr() method, But in this case there is a limits on most sizes.

## Repr Objects

The Repr object provides several attributes, which can be used to provide the size limits for different datatypes.

repr.maxlevel represents the depth limit for the recursive representation. The default value is 6. There are some other max limits for dict, lists, tuples, sets etc.

Sr.No. Methods/Variables & Description
1

Repr.maxlong

Maximum number to represent long value. Default is 40

2

Repr.maxstring

Limit number of characters in a string type object. Default is 30.

3

Repr.maxother

Limit the size of some other data, where formatting is not specified.

4

Repr.repr(obj)

Same as built-in repr() method

5

Repr.repr1(obj, level)

Recursive implementation of repr(). Also specify level for the recursive output

## Example Code

import reprlib
import math
fact_list = [math.factorial(x) for x in range(50)]
print(reprlib.repr(fact_list))
myRept = reprlib.Repr() #Create Repr object and set long size to 10
myRept.maxlong = 10
print(myRept.repr(fact_list[23]))


## Output

[1, 1, 2, 6, 24, 120, ...]
258...0000

Published on 31-Oct-2018 11:51:21