zoukankan      html  css  js  c++  java
  • python 总结之 repr函数

    repr:

    repr(object)

    Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.

    可以认为它能把一个对象转换成一个可打印的字符串,它特点是对于包含着转义字符的字符串,也能完整打印出来,例如

    l = [ 'cdxc3kx9bx1dx08x84MXr0!xd1xecx86xabx01', 'cwx1cx9ex08x05`x8fx9dfdxd0xbfS7x9dZx8f']

    直接 print l[i],就会出现乱码,原因是里面的元素含义转义字符

    这时候采用repr的函数,它就能完成地把转义字符也打印出来

    --------------------------我是测试输出结果的分隔线-----------------------------------------------------------------------

    l = [ 'cdxc3kx9bx1dx08x84MXr0!xd1xecx86xabx01', 'cwx1cx9ex08x05`x8fx9dfdxd0xbfS7x9dZx8f']

    printl[0]

    print repr(l[0])

    >>>
    cd胟?凪Xr0!鸯啱
    'cdxc3kx9bx1dx08x84MXr0!xd1xecx86xabx01'

  • 相关阅读:
    java中文api 安装和引入eclipse
    【JAVA学习笔记】Java中的static关键字解析
    终于做好了JDBC在java和sql 2005之间的连接了 ,快哭了
    CF 5 A. Chat Server's Outgoing Traffic
    NYOJ 16 矩形嵌套
    poj 1061 青蛙的约会
    nefu 2 猜想
    nefu 120 梅森素数
    nefu 118 n!后面有多少个0
    nefu 117 素数个数的位数
  • 原文地址:https://www.cnblogs.com/tanliyu/p/4933752.html
Copyright © 2011-2022 走看看