How to debug python program using repr

Generally, when debugging a program, print is more inclined to use direct printing to make judgments, but print can only print out the results, and cannot judge the type. E.g:

Copy code
a = 5
b = ‘5’
print(a)
print(b)

The result is:
5
5
Copy code
For a and b are the same on the surface, it may default to a == b

In fact, a is of type int and b is of type string

Use repr to see the result:

Copy code
a = 5
b = ‘5’
print(repr(a))
print(repr(b))

The result is:
5
‘5’
Copy code
For dynamic python objects, it is also very convenient to use repr:

Copy code
class OpenClass(object):
def init(self, x, y):
self.x = x
self.y = y

obj = OpenClass(2,3)
print(obj)

The result is: <main.OpenClass object at 0x101cb7390

Copy code
Rebuild the object with repr:

Copy code
class OpenClass(object):
def init(self, x, y):
self.x = x
self.y = y

def repr(self):
return ‘OpenClass(%d,%d)’%(self.x, self.y)
obj = OpenClass(2,3)
print(obj)

The result is: OpenClass(2,3)

Copy code
For print, only easy-to-read information can be printed, but the type cannot be displayed

repr displays the type and concisely displays the data information

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

How to debug python program using repr
How to save the python program
How to program based on interfaces in Python
How to switch the hosts file using python
How to write a confession program in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand python objects
How to use python tuples
Python| function using recursion to solve
python how to view webpage code
How to use python thread pool
How to write python configuration file
How to wrap in python code
Using Python to implement multiple clipboards
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to install Python on CentOS 8
How to solve python dict garbled
How to view errors in python
How to write return in python
How to view the python module
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
How to use SQLite in Python
How to make a globe with Python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to represent null values in python
How to save text files in python
How to use PYTHON to crawl news articles
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
R&amp;D: How To Install Python 3 on CentOS 7
How to add custom modules in Python
How to process excel table with python
How to view installed modules in python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to get started quickly with Python
How to enter python triple quotation marks
How to add background music in python
How to represent relative path in python
How to upgrade all Python libraries on Ubuntu 18.04
How does Python call cmd using OS modules
How to use the round function in python
How to convert web pages to PDF with Python
How to install Java on Ubuntu 16.04 using Apt-Get
How to use the zip function in Python
How to install python in ubuntu server environment