python __str__repr__ 区别
__str__
__repr__
两个内置函数都是调试常用的函数, 对象直接调用时会调用 __repr__的内容, __str__需要print一下对象才可以
两个函数的内容有时会写成相同内容 __repr__ = __str__
>>> class Test(object):
... def __init__(self, value='hello world!'):
... self.data = value
...
...
>>> class TestRepr(Test):
... def __str__(self):
... return "TestRepr(%s)--str" %self.data
... def __repr__(self):
... return "TestRepr(%s)--repr" %self.data
...
>>> test = TestRepr()
>>> test
TestRepr(hello world!)--repr
>>> print test
TestRepr(hello world!)--str
python __str__repr__ 区别的更多相关文章
- C语言和python的区别
Python可以说是目前最火的语言之一了,人工智能的兴起让Python一夜之间变得家喻户晓,Python号称目前最最简单易学的语言,现在有不少高校开始将Python作为大一新生的入门语言.本萌新也刚开 ...
- Python学习---Java和Python的区别小记
Java和Python的区别小记 注意这里使用的是 and/or/not 非java中的&&,||,!Java中的true是小写 Python中函数就是对象,函数和我们之前的[1,2 ...
- 【python】ipython与python的区别
[python]ipython与python的区别 (2014-06-05 12:27:40) 转载▼ 分类: Python http://mba.shengwushibie.com/itbook ...
- Python - __name__=='__main__'是干啥的,以及python -m与python的区别
1. __name__=='__main__'是干啥的 先看例子,准备a.py和b.py放在同一个文件夹中 vi a.py # coding: utf-8 print("i am just ...
- python语法区别
python语法区别: 大小写敏感 (动态语言:python)变量不用声明 p.s: 静态语言(Java)必须声明变量 语句末尾可以不打分号 可以直接进行数学计算 复制.粘贴功能失效,粘贴到别的地方的 ...
- python 2 与python 3区别汇总
python 2 与python 3区别汇总 一.核心类差异1. Python3 对 Unicode 字符的原生支持.Python2 中使用 ASCII 码作为默认编码方式导致 string 有两种类 ...
- ipython与python的区别
http://mba.shengwushibie.com/itbook/BookChapter.asp?id=8745 http://www.cnblogs.com/yangze/archive/20 ...
- python中#!/usr/bin/python与#!/usr/bin/env python的区别
目的是在运行python脚本的时候告诉操作系统我们要用python解释器去运行py脚本 所以我们在第一句往往会写如下两句中的其中一句: #!/usr/bin/python 或 >#!/usr/b ...
- java的this static public protected private abstract interface 在python的对应,java python一些区别
1.因为工作的原因,最近使用了三个多月的java作为主力语言.很早之前在菜鸟教程也看过java文档两遍,但实践少,处于能看懂写出来不流畅的状态(对于java必须要略懂,不能能看到就头疼跳过,因为现在百 ...
随机推荐
- UTC和GMT
UTC GMT UTC = Coordinated Universal Time. 中文名称为协调世界时. GMT = Greenwich Mean Time. 中文名称为格林尼治(平)时 UTC = ...
- [luogu]P1600 天天爱跑步[LCA]
[luogu]P1600 [NOIP 2016]天天爱跑步 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上 ...
- CF 546 B Soldier and Badges(贪心)
原题链接:http://codeforces.com/problemset/problem/546/B 原题描述: Soldier and Badges Colonel has n badges. H ...
- canvas 时钟案例
<!doctype html><html><head> <meta charset="UTF-8"> <meta name=& ...
- matlab 重命名文件和文件夹
1.查看文件存在 dir() 若存在,返回文件信息 dir(‘test.txt’) %查看当前目录是否存在test.txt文件 dir(‘C:\test.txt’) %查看指定目录是否存在 ...
- react-native 组件之间传值
1.通过 AsyncStorage 将值保存在本地(最低端的方法) import { AsyncStorage, } from 'react-native'; // 保存 IP AsyncStorag ...
- leetcode-mid-array-3 Longest Substring Without Repeating Characters
mycode 99.21% class Solution(object): def lengthOfLongestSubstring(self, s): """ :ty ...
- 在vue中如何引jquer
最近学习vue,习惯性的通过<script>标签引入jquery,写完后报错才想起来,这种方式在vue是不适用的. 1:因为已经安装了vue脚手架,所以需要在webpack中全局引入jqu ...
- 三十四、python中shutil模块的介绍
'''A.shutil:高级的文件 文件夹 压缩包 处理模块''' import shutil '''1.copyfileobj(a1,a2,lenth):将文件内容拷贝到另一个文件中''' shut ...
- tikz: keep in mind these
don't add % in title, xlabel, ylabel etc., use \%