• Python中str()与__str__、repr()与__repr__区别与关系


    str()与__str__、repr()与__repr__ 的区别:

      str()与repr()都是python中的内置函数,是直接用来格式化字符串的函数。

      __str__与__repr__ 是在类(对象)中对类(对象)本身进行字符串处理。

    str

     1 >>>help(str)
     2 Help on class str in module builtins:
     3 
     4 class str(object)
     5  |  str(object='') -> str
     6  |  str(bytes_or_buffer[, encoding[, errors]]) -> str
     7  |
     8  |  Create a new string object from the given object. If encoding or
     9  |  errors is specified, then the object must expose a data buffer
    10  |  that will be decoded using the given encoding and error handler.
    11  |  Otherwise, returns the result of object.__str__() (if defined)
    12  |  or repr(object).
    13  |  encoding defaults to sys.getdefaultencoding().
    14  |  errors defaults to 'strict'.

      从给定对象创建一个新字符串对象。

      如果指定了编码或错误,则对象必须公开一个将使用给定的编码和错误处理程序进行解码的数据缓冲区。

      否则,返回对象的结果。

      __str__()(如果定义)或代表(对象)。默认getdefaultencoding()编码系统。错误默认为“严格”。

    repr

    1  >>>help(repr)
    2 
    3  Help on built-in function repr in module builtins:
    4  
    5  repr(obj, /)
    6      Return the canonical string representation of the object.
    7  
    8      For many object types, including most builtins, eval(repr(obj)) == obj.

      首先,尝试生成这样一个字符串,将其传给 eval()可重新生成同样的对象 

      否则,生成用尖括号包住的字符串,包含类型名和额外的信息(比如地址)

     eval 

     1 >>>help(eval)
     2 
     3 Help on built-in function eval in module builtins:
     4 
     5 eval(source, globals=None, locals=None, /)
     6     Evaluate the given source in the context of globals and locals.
     7 
     8     The source may be a string representing a Python expression
     9     or a code object as returned by compile().
    10     The globals must be a dictionary and locals can be any mapping,
    11     defaulting to the current globals and locals.
    12     If only globals is given, locals defaults to it.

    代码如下:

     1 >>> eval('1+2')
     2 3
     3 >>> str('1+2')
     4 '1+2'
     5 >>> repr('1+2')
     6 "'1+2'"
     7 >>> type(repr('1+2'))
     8 <class 'str'>
     9 >>> type(str('1+2'))
    10 <class 'str'>
    11 >>> type(eval('1+2'))
    12 <class 'int'>
    13 #就相当于上面说的eval(repr(object)) == object
    14 >>> eval(repr('1+2'))     
    15 '1+2'
    16 >>> '1+2'
    17 '1+2' 

    实例:

      Python 定义了__str__()和__repr__()两种方法,__str__()用于显示给用户,而__repr__()用于显示给开发人员。

     1 class Person(object):
     2     def __init__(self, name, sex):
     3         self.name = name
     4         self.sex = sex
     5     def __str__(self):
     6         return '(Person: %s, %s)' % (self.name, self.sex)
     7 
     8 class Student(Person):
     9     def __init__(self, name, sex, score):
    10         Person.__init__(self, name, sex)
    11         self.score = score
    12     def __str__(self):
    13         return '(Student: %s, %s, %s)' % (self.name, self.sex, self.score)
    14     def __repr__(self):
    15         return '(Student: %s, %s, %s)' % (self.name, self.sex, self.score)
     1 >>> from demo import Person, Student
     2 >>> p = Person('Alice', 'Female')
     3 >>> p
     4 <demo.Person object at 0x103eac0b8>
     5 >>> print (p)
     6 (Person: Alice, Female)
     7 >>> s = Student('Tom', 'male', 20)
     8 >>> s
     9 (Student: Tom, male, 20)
    10 >>> print (s)
    11 (Student: Tom, male, 20)
  • 相关阅读:
    hdu 3722 Card Game 二分图的最大权匹配
    如何将中国知网CNKI中的文献导入EndNote X6
    高性能以太网芯片W5500 数据手册 V1.0(二)
    (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
    [置顶] 斗地主算法的设计与实现--项目介绍&如何定义和构造一张牌
    Robotium学习笔记一
    Robotium学习笔记二
    Robotium学习笔记三
    做一个测试经理我还差多少?
    [每日一题] 11gOCP 1z0-053 :2013-10-9 backup with the KEEP option....................................33
  • 原文地址:https://www.cnblogs.com/mayunting/p/8251518.html
Copyright © 2020-2023  润新知