Python-printr for own objects of a class instance

Python-printr is a module that allows to emulate the print_r() function of PHP by printing the objects properties of a class instance and its internal structure. If you need to use printr() function for list, tuples and dicts, please, don't use printr() function. You can choose pprint module. printr() function is only for objects of your own class instances.

Learning about Python-printr

Python-printr download

You can copy the source code on your own printr.py file or download file from this link.

Python-printr licence & copyright

Developed by Eugenia Bahit (Software Architect, GLAMP Teacher & Agile Coach).
Distributed under a GPL v3.0 licence.

Python-printr documentation

printr is a module that allows to emulate the print_r() function of PHP by printing the objects properties of a class instance and its internal structure.

Using printr module for Python

First, you must get an object of a class instance and then, you can call to the printr function by passing the object like an argument function.

Example:

# -*- coding: utf-8 -*-


class Vidrio(object):

    def __init__(self):
        self.color = ''
        
        
class Marco(object):

    def __init__(self):
        self.color = ''
        self.material = ''
        self.vidrio = Vidrio()


class Ventana(object):
    
    def __init__(self):
        self.posicion = ''
        self.marco = Marco()


from printr import printr
ventana = Ventana()
printr(ventana)


Returns:

<Ventana object>
    {
        posicion: ''
        marco: <Marco object>
        {
            color: ''
            vidrio: <Vidrio object>
            {
                color: ''
            }
            material: ''
        }
    }

Advanced use of Python-printr

You can set the character using for identation. By default is white space. Change this value by modifying the IDENTATION_CHAR constant:

import printr
printr.IDENTATION_CHAR = "."  # use point to identation
myobject = MyClass()
printr.printr(myobject)

Also, you can set the identation width, by modifying the TAB_WIDTH constant (by default is 4):

import printr
printr.TAB_WIDTH = 2
myobject = MyClass()
printr.printr(myobject)