Convention for printing an object in python

Is there a standard convention for printing an object in python. I know that if I just try to print the object, it will print the memory address, but I would like to rewrite this method and be able to print a readable format of the object to help with debugging.

Is there any standard convention that people adhere to, or is this not the best way to define such a method, are there better alternatives instead?

+4
source share
4 answers

You can overwrite either the __str__ function or __repr__ .

There is no agreement on how to implement the __str__ function; it can simply return any readable representation of the string you want. However, there is an agreement on how to implement the __repr__ function: it must return a string representation for the object so that the object can be recreated from this representation (if possible), i.e. eval(repr(obj)) == obj .

Assuming you have a Point class, __str__ and __repr__ can be implemented as follows:

 class Point: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return "(%.2f, %.2f)" % (self.x, self.y) def __repr__(self): return "Point(x=%r, y=%r)" % (self.x, self.y) def __eq__(self, other): return isinstance(other, Point) and self.x == other.x and self.y == other.y 

Example:

 >>> p = Point(0.1234, 5.6789) >>> str(p) '(0.12, 5.68)' >>> "The point is %s" % p # this will call str 'The point is (0.12, 5.68)' >>> repr(p) 'Point(x=0.1234, y=5.6789)' >>> p # echoing p in the interactive shell calls repr internally Point(x=0.1234, y=5.6789) >>> eval(repr(p)) # this echos the repr of the new point created by eval Point(x=0.1234, y=5.6789) >>> type(eval(repr(p))) <class '__main__.Point'> >>> eval(repr(p)) == p True 
+7
source

Add __str__ to the class for the object you are printing.

If you print objects for a class that you cannot change, it is fairly simple to provide your own print function, since you are using Python 3.

Edit: Normally, the string returned by __str__ will be class specific, but at least enough to identify the object. The exact format of the string will depend on the class and public attributes.

Edit2: Here is a simple example (example) from a class that describes countries:

 def __str__(self): return "{0:<32} {1:>010}". format(self.__name, self.__population) 
+5
source

If your object can be represented in a way that allows you to relax, override the __repr__ function. For example, if you can create your object with the following code:

 MyObject('foo', 45) 

__repr__ should return "MyObject('foo', 45)" . Then you do not need to implement __str__ .

But if the object is so complex that you cannot imagine it, replace __str__ . Then you should return something that both makes it clear that the object cannot be recreated and that it is an object. Therefore, do not return "foo:45" because it looks like a string, or {'foo': 45} because it looks like a dictionary, and it will confuse you when debugging.

I would recommend keeping the brackets, for example <MyObject foo:45> . Thus, it is clear that you printed the object, and it is also clear that it is not only about creating MyObject('foo', 45) to recreate the object, but also to store more data.

+3
source

The standard way to print user information about an object (an instance of a class) is to use the __str__ method:

 class A: var = 1 def __str__(self): return 'Accessing from print function, var = {0}'.format(self.var) 

In this method, you can display any information you want

 a = A() print(a) >>> Accessing from print function, var = 1 
+1
source

Source: https://habr.com/ru/post/1444059/


All Articles