Python内置函数(30)——hex
英文文档:
hex(x)
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”, for example
If x is not a Python int object, it has to define an __index__() method that returns an integer.
说明:
1. 函数功能将10进制整数转换成16进制整数。
>>> hex(15)
'0xf'
>>> hex(16)
'0x10'
2. 如果参数x不是整数,则它必须定义一个返回整数的__index__函数。
# 未定义__index__函数
>>> class Student:
def __init__(self,name,age):
self.name = name
self.age = age >>>
>>> s = Student('Kim',10)
>>> hex(s)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
hex(s)
TypeError: 'Student' object cannot be interpreted as an integer # 定义__index__函数,但是返回字符串
>>> class Student:
def __init__(self,name,age):
self.name = name
self.age = age
def __index__(self):
return self.name >>> s = Student('Kim',10)
>>> hex(s)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
hex(s)
TypeError: __index__ returned non-int (type str) # 定义__index__函数,并返回整数
>>> class Student:
def __init__(self,name,age):
self.name = name
self.age = age
def __index__(self):
return self.age >>> s = Student('Kim',10)
>>> hex(s)
'0xa'
Python内置函数(30)——hex的更多相关文章
- Python内置函数(20)——hex
英文文档: hex(x) Convert an integer number to a lowercase hexadecimal string prefixed with "0x" ...
- Python内置函数(30)——super
英文文档: super([type[, object-or-type]]) Return a proxy object that delegates method calls to a parent ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
- python内置函数简单归纳
做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
随机推荐
- python书籍推荐:Python Cookbook第三版中文
所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/44/ 来源:python黑洞网 内容 ...
- LNMP环境并发优化
LNMP环境并发优化 服务器 8核32Gx3 如图是一条http请求的生命周期,共经过nginx,php-fpm,PHP三个模块 所以我们可以从nginx,php-fpm,PHP三个维度去优化 一.p ...
- HBASE强制删除表
1,先把hdfs的对应表的数据删除 hadoop fs -mv /hbase/<table_name> /tmp 2,修复meta信息 hbase hbck -fixMeta -fixAs ...
- Python序列化proto中repeated修饰的数据
一.repeated修饰复合数据结构,即message时 1.使用message的add方法初始化新实例 2.分别对新实例中的每个元素赋值:或使用CopyFrom(a)拷贝a中的元素值 message ...
- jqurey.running.min.js运动的字体效果
参考网址: http://yanshi.sucaihuo.com/jquery/22/2226/demo/ 里面有详细的解释 下面是案例效果demo,其中jquery.running.css与jque ...
- URI is not registered ( Setting | Project Settings | Schemas and DTDs )
URI is not registered ( Setting | Project Settings | Schemas and DTDs ) 在idea中,当初手动第一次写spring配置文件的时候 ...
- 查找更改的PeopleCode
当我们做工程包迁移时,经过会遗漏部分更改过的定义.我们可以用下面的SQL来查找变更项 变量 &OPRID =代码变更者 变量 &PROJECT 项目工程名 SELECT * FROM ...
- oracle基础语句练习
1. 创建相关表结构 Emp----员工信息表 Ename ), --姓名 Empno ), --编号 Deptno ), --所在部门 Job ), --工种(人员类别),如:manager 经理, ...
- NPOI操作创建Excel
一.下载NPOI类库 使用Nuget在线搜索NPOI,下载安装 二.代码开撸 var workBook = new HSSFWorkbook(); #region 设置样式 IFont font = ...
- java 根据某个数字,计算前后多少天的具体日期
import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Calendar; import ...