Showing posts from July, 2015

Simple Python __str__(self) method for use during development

For my development work I want a simple way to display the data in an object instance without having to modify the __str__(self) method every time I add, delete, or rename members. Here's a technique I've adopted that relies on the fact that every object stores all of its members in a dictionary called self.__dict__ . Making a string representation of the object is just a matter of returning a string representation of __dict__ . This can be achieved in several ways. One of them is simply str(self.__dict__) and the other uses the JSON serializer json.dumps() , which lets you prettyprint the result. Here's a little Python demonstrator program: # /usr/bin/python """ demo - demonstrate a simple technique to display text representations     of Python objects using the __dict__ member and a json serializer.     $Id:,v 1.3 2015/07/18 13:07:15 marc Exp marc $ """ import json class something(object):     """ This is just a demon