Python内置函数(20)——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.
将整数转换为16进制的字符串
说明:
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内置函数(20)——hex的更多相关文章
- Python内置函数(20)——exec
英文文档: exec(object[, globals[, locals]]) This function supports dynamic execution of Python code. obj ...
- Python内置函数(30)——hex
英文文档: hex(x) Convert an integer number to a lowercase hexadecimal string prefixed with “0x”, for exa ...
- 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内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
- lambda 表达式+python内置函数
#函数 def f1(a,b): retrun a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...
- 学习过程中遇到的python内置函数,后续遇到会继续补充进去
1.python内置函数isinstance(数字,数字类型),判断一个数字的数字类型(int,float,comple).是,返回True,否,返回False2.python内置函数id()可以查看 ...
随机推荐
- Learn HTML5 in 5 Minutes!
There's no question, HTML5 is a hot topic for developers. If you need a crash course to quickly unde ...
- 20165230 2017-2018-2 《Java程序设计》第3周学习总结
20165230 2017-2018-2 <Java程序设计>第3周学习总结 教材学习内容总结 本周主要学习了类与对象. 包括创建对象与构造方法. 了解了程序是由若干个类所构成:类分为类名 ...
- [Oracle] UNIX与Windows 2000上Oracle的差异(III)
作者:Ian Adam & David Stien, SAIC Ltd 日期:19-Dec-2003 出处:http://www.dbanotes.net翻译:Fenng ORACLE 的安装 ...
- LinkedList源码解析(JDK1.8)
package java.util; import java.util.function.Consumer; /** * LinkedList是List和Deque接口的双向链表的实现.实现了所有可选 ...
- C++模板类与Qt信号槽混用
一.正文 目前正在做一个视频处理相关的项目.项目的技术栈是这样的,UI层采用Qt来实现基本的数据展示和交互,底层音视频采用的是一套基于FFmpeg的视频处理框架.这是一套类似Microsoft Med ...
- [POJ 3764] The xor-longest Path
Description 多组数据 给你一颗树, 然后求一条最长异或路径, 异或路径长度定义为两点间简单路径上所有边权的异或和. Solution 首先 dfs 一遍,求出所有的点到根节点(随便选一个) ...
- Java Arrays 源码 笔记
Arrays.java是Java中用来操作数组的类.使用这个工具类可以减少平常很多的工作量.了解其实现,可以避免一些错误的用法. 它提供的操作包括: 排序 sort 查找 binarySearch() ...
- 【Python】 命名空间与LEGB规则
命名空间与LEGB规则 之前隐隐约约提到过一些关于Python赋值语句的特殊性的问题,这个问题的根源就在于Python中的变量的命名空间机制和之前熟悉的C也好java也好都不太一样. ■ 命名空间 所 ...
- JVM学习七:JVM之类加载器之类的卸载
类加载的过程和原理,以及双亲委派机制都已经讲解完成,那么我们今天讲解类加载的最后一节,那么就是类的卸载. 我们知道,当一个类被加载.连接和初始化之后,他的生命周期就开始了,当该类的class对象不再被 ...
- Java几种建立实例的方法
1.new something obj=new something(); 2.clone 根据现有的对象实例来建立里一个新对象实例. 只限于实现了java.lang.Cloneable接口的类.否 ...