python-repr()和val()函数
1. repr() 函数将对象转化为供解释器读取的形式。
语法
以下是 repr() 方法的语法:
repr(object)
参数
- object -- 对象。
返回值
返回一个对象的 string 格式。
str和repr都是用来将数字,列表等类型转化为字符串的形式,但不同之处在于str更加类似于C语言中使用printf输出的内容,而repr输出的内容会直接将变量的类型连带着表现出来,从下图可以看出,对明显带有类型标志的变量而言,str和repr的转换具有明显的差别,如long型数字和字符串的‘’符号,而对于并没有非常大区别的记录数据如整型数字,二者并没有太大的差别。
2. eval(str)函数很强大,官方解释为:将字符串str当成有效的表达式来求值并返回计算结果。所以,结合math当成一个计算器很好用。
eval()函数常见作用有:
1、计算字符串中有效的表达式,并返回结果
>>> eval('pow(2,2)')
4
>>> eval('2 + 2')
4
>>> eval("n + 4")
85
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2、将字符串转成相应的对象(如list、tuple、dict和string之间的转换)
>>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>> b = eval(a)
>>> b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> a = "{1:'xx',2:'yy'}"
>>> c = eval(a)
>>> c
{1: 'xx', 2: 'yy'}
>>> a = "(1,2,3,4)"
>>> d = eval(a)
>>> d
(1, 2, 3, 4)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
3、将利用反引号转换的字符串再反转回对象
>>> list1 = [1,2,3,4,5]
>>> `list1`
'[1, 2, 3, 4, 5]'
>>> type(`list1`)
<type 'str'>
>>> type(eval(`list1`))
<type 'list'>
>>> a = eval(`list1`)
>>> a
[1, 2, 3, 4, 5]
e.g. Your task is to define the following two methods for the Coordinate class:
Add an
__eq__method that returns True if coordinates refer to same point in the plane (i.e., have the same x and y coordinate).Define
__repr__, a special method that returns a string that looks like a valid Python expression that could be used to recreate an object with the same value. In other words,eval(repr(c)) == cgiven the definition of__eq__from part 1.
class Coordinate(object):
def __init__(self,x,y):
self.x = x
self.y = y
def getX(self):
# Getter method for a Coordinate object's x coordinate.
# Getter methods are better practice than just accessing an attribute directly
return self.x
def getY(self):
# Getter method for a Coordinate object's y coordinate
return self.y
def __str__(self):
return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'
def __eq__(self, other):
# First make sure `other` is of the same type
assert type(other) == type(self)
# Since `other` is the same type, test if coordinates are equal
return self.getX() == other.getX() and self.getY() == other.getY()
def __repr__(self):
return 'Coordinate(' + str(self.getX()) + ',' + str(self.getY()) + ')'
Test: equal 1
- Output:
-
> print(c1)
<1,-8>
> print(c2)
<1,-8>
> print(c1 == c2)
True
Test: equal 2
- Output:
-
> print(c1)
<20,20>
> print(c2)
<20,20>
> print(c1 == c2)
True
Test: not equal 1
- Output:
-
> print(c1)
<-16,-4>
> print(c2)
<14,20>
> print(c1 == c2)
False
Test: not equal 2
- Output:
-
> print(c1)
<7,13>
> print(c2)
<-2,-1>
> print(c1 == c2)
False
Test: repr
- Output:
-
> print(c1)
<17,38>
> print(repr(c1))
Coordinate(17,38)
Test: repr randomized
- Output:
-
> print(c1)
<-12,-20>
> print(repr(c1))
Coordinate(-12,-20)
python-repr()和val()函数的更多相关文章
- Python repr() 或str() 函数(转)
Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数.函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式(如果没有等价的语法,则会发生 ...
- Python repr() 函数
Python repr() 函数 Python 内置函数 描述 repr() 函数将对象转化为供解释器读取的形式. 语法 以下是 repr() 方法的语法: repr(object) 参数 obje ...
- python中常用的函数与库一
1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作 ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- 如何查看Python的内置函数
经常调用的时候不知道python当前版本的内置函数是哪些,可以用下面的指令查看: C:\Users\Administrator>python Python 2.7.11 (v2.7.11:6d1 ...
- python常用内置函数
Python所以内置函数如下: 下面列举一些常用的内置函数: chr()和ord() chr()将数字转换为对应的ascii码表字母 >>> r=chr(65) >>&g ...
- python基础——内置函数
python基础--内置函数 一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...
- Python的内置函数
python的内置函数一共有68个,下面将简单介绍各个函数的功能. abs() dict() help() min() setattr() all() dir() hex() next() slice ...
- Python _内置函数3_45
reversed: #reversed() l = [1,2,3,4,5] l.reverse() print(l) #改变了原来的列表 l = [1,2,3,4,5] l2 = reversed(l ...
- python进阶之魔法函数
__repr__ Python中这个__repr__函数,对应repr(object)这个函数,返回一个可以用来表示对象的可打印字符串.如果我们直接打印一个类,向下面这样 class A(): ...
随机推荐
- 卡尔曼滤波总结——KF、EFK、UKF
1.用途 现实是我们的处理和测量模型都是非线性的,结果就是一个不规则分布,KF能够使用的前提就是所处理的状态是满足高斯分布的,为了解决这个问题,EKF是寻找一个线性函数来近似这个非线性函数,而UKF就 ...
- PrimeNG01 angular集成PrimeNG
1 开发环境 本博文基于angular5 2 步骤 2.1 创建angular5项目 详情参见百度 2.2 下载PrimeNG依赖 npm install primeng --save npm ins ...
- list 的扩展
数据挖掘中会遇到添加多个新的特征s,对一个feature = list()来说, 除了可以用 feature.append('xx') # 在尾部添加一个特征 feature.extend(['xx' ...
- Python 之 文件处理
文件操作: 文件路径:d:\文件名.txt 编码方式:utf-8.gbk 操作方式:只读.只写.追加.读写.写读... 只读:r 或 rb #相对路径 f=open("文件名& ...
- Xcode弱网测试工具
Network Link Conditioner Network Link Conditioner工具是Mac下提供的一个弱网测试工具. 安装Network Link Conditioner Xcod ...
- [GO]随机生成四们数字
package main import ( "math/rand" "time" "fmt" ) func InitData(p *int) ...
- 深入理解python中可迭代对象,迭代器,生成器
英文原文出处:Iterables vs. Iterators vs. Generators 在python学习中,通常会陷入对以下几个相关概念之间的确切差异的困惑中: a container(容器) ...
- 排序算法汇总(java实现,附源代码)
整理系统的时候发现了原来写的各种算法的总结,看了一下,大吃一惊,那时候的我还如此用心,具体的算法,有的已经模糊甚至忘记了,看的时候就把内容整理出来,顺便在熟悉一下,以后需要的时候就可以直接过来摘抄了. ...
- .netcore部署到IIS上出现HTTP Error 502.5 - Process Failure问题解决
首先网上是有很多解决方案,但是对我这个错误完全没用 如果你们没有环境首先得预装环境如下 1.首先在bing.com下搜索asp.net core download, 然后打开搜索到的信息.NET Do ...
- 【Linux-学习笔记-不定期更新】
command--help ./当前的路径 目录操作命令: mkdir 创建目录: 创建多级目录 : mkdir -p 查看目录:ls ls -a:显示所有文件,包括隐藏文件 隐藏文件以.开头 ls ...