zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python中repr函数作用是什么?

2023-03-20 15:26:07 时间

python中repr函数作用是什么?

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

1、repr() 函数

得到的字符串通常可以用来重新获得该对象,将对象转化为供解释器读取的形式。

2、语法

repr(a)

3、参数

a: 对象。

4、返回值

返回一个对象的 string 格式。

5、使用实例

class test:
    def __init__(self,name,age):
        self.age = age
        self.name = name
    def __repr__(self):
        return "Class_Test[name="+self.name+",age="+str(self.age)+"]"
t = test("Zhou",30)
 
print(t)

repr()的输入对python比较友好,大家可以放心使用哦~