本系列将会陆续整理分享一些的Python内置函数。
-
通过百度网盘获取:
链接:https://pan.baidu.com/s/11x9_wCZ3yiYOe5nVcRk2CQ?pwd=mnsj
提取码:mnsj
-
前往GitHub获取:
https://github.com/returu/Python_built-in_functions
repr(object)
-
object:任何 Python 对象。
返回值:
下面是一些使用 repr() 函数的示例:
-
示例 1:
# 内置类型
print(repr(42)) # 输出: '42'
print(repr("hello")) # 输出: "'hello'"
print(repr([1, 2, 3])) # 输出: '[1, 2, 3]'
print(repr({'a': 1})) # 输出: "{'a': 1}"
x = [1, 2, 3]
x == eval(repr(x))
# 输出: True
-
示例 2:
class MyClass:
pass
obj = MyClass()
print(repr(obj))
# 输出: <__main__.MyClass object at 0x000001B22680FA70>
-
示例 3:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point(x={self.x}, y={self.y})"
p = Point(1, 2)
print(repr(p))
# 输出: 'Point(x=1, y=2)'


本篇文章来源于微信公众号: 码农设计师