python 重写__repr__与__str__函数

‘‘‘重写:将函数重写定义写一遍

__str__():在调用print打印对象时自动调用,是给用户用的,是一个描述对象的方法。__repr__():是给机器用的,在Python解释器里面直接敲对象名在回车后调用的方法注意:在没有str时,且有repr,str = repr

‘‘‘class Person(object):    def __init__(self, name, age, height, weight):        self.name = name        self.age = age        self.height = height        self.weight = weight    def __str__(self):        return "%s-%d-%d-%d" % (self.name, self.age, self.height, self.weight)

per = Person("hanmeimei", 20, 170, 55)#print(per.name, per.age, per.height, per.weight)print(per)

#有点:当一个对象的属性值很多,并且都需要打印,重写了__str__方法后,简化了代码

原文地址:https://www.cnblogs.com/pygo/p/12292692.html

时间: 2024-07-31 12:57:52

python 重写__repr__与__str__函数的相关文章

Python中__repr__和__str__区别

如下: Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) Type "help", "copyright", "credits" or "license" for >>> class Test(object): ... def __init__(self, value='hello, world!'): ... self.data = value ...

Python中__repr__和__str__区别(转)

class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Test() >>> t <__main__.Test at 0x7fa91c307190> >>> print t <__main__.Test object at 0x7fa91c307190> # 看到了么?上面打印类对象并不是很友好,显示的是对象

Python __str__函数

class Cat: def __init__(self,_name): self.name = _name def __str__(self): return "i am %s"%self.name def show(self): print("name is %s"%self.name) tom = Cat("tom") tom.show() print(tom) print("------------"); lanmao

python字符串格式化方法 format函数的使用

python从2.6开始支持format,新的更加容易读懂的字符串格式化方法, 从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序号, 或者 变量名直接引用. 从format参数引入的变量名 . 冒号:. 字符位数声明. 空白自动填补符 的声明 千分位的声明 变量类型的声明: 字符串s.数字d.浮点数f 对齐方向符号 < ^ > 属性访问符中括号 ? 使用惊叹号!后接a .r. s,声明 是使用何种模式, acsii模式.引用_

python笔记_magic变量和函数

前言 先扯一点背景知识 PEP8(Python Enhancement Proposal)是一份python的编码规范,链接:http://www.python.org/dev/peps/pep-0008/ 在这份编码规范中的“命名规范-命名风格”这一节的最后,提到了对几种使用前置和后置下划线的,对变量的比较特殊的命名方式: 单下划线开头:弱内部使用标识,无法被from M import *所引用 单下划线结尾:避免和python关键字冲突,可以加个后置下划线 双下划线开头:类成员变量中的私有变

Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因

Python重写C语言程序100例--Part8

''' [程序61] 题目:打印出杨辉三角形(要求打印出10行如下图) 1.程序分析: ''' if __name__ == '__main__': a = [] for i in range(10): a.append([]) for j in range(10): a[i].append(0) for i in range(10): a[i][0] = 1 a[i][i] = 1 for i in range(2,10): for j in range(1,i): a[i][j] = a[i

Python重写C语言程序100例--Part10

软中断 软中断的分配时静态的(即在编译时定义),而tasklet的分配和初始化可以在运行时进行. 软中断(即便是同一种类型的软中断)可以并发地运行在多个CPU上.因此,软中断是可重入函数而且必须明确地使用自旋锁保护其数据结构.tasklet不必担心这些问题,因为内核对tasklet的执行进行了更加严格的控制.相同类型的tasklet总是被串行执行. 换句话说就是:不能在两个CPU上同时运行相同类型的tasklet.但是,类型不同的tasklet可以在几个CPU上并发执行.tasklet的串行化使

Python重写C语言程序100例--Part11

''' [程序91] 题目:时间函数举例1 1.程序分析: 2.程序源代码: ''' if __name__ == '__main__': import time print time.ctime(time.time()) print time.asctime(time.localtime(time.time())) print time.asctime(time.gmtime(time.time())) ''' [程序92] 题目:时间函数举例2 1.程序分析: 2.程序源代码: ''' if