Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses repr

Shulou Source: shulou.com Published: 2022-06-01 06:00:12 10月18日 Update

This article will explain in detail how python uses repr. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Repr

When defining a class or object in Python, it is useful to provide an "official" way to represent the object as a string.

For example:

> file = open ('file.txt', 'r') > print (file)

This makes debugging code easier. Add it to your class definition, as follows:

Class someClass: def _ _ repr__ (self): return "" someInstance = someClass () # print print (someInstance) this is the end of the article on "how python uses repr". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Articles objects more good practical code content characters strings official articles methods useful knowledge reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info OPPO Reno Xiaomi MariaDB