英文文档:

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的更多相关文章

  1. Python内置函数(20)——exec

    英文文档: exec(object[, globals[, locals]]) This function supports dynamic execution of Python code. obj ...

  2. Python内置函数(30)——hex

    英文文档: hex(x) Convert an integer number to a lowercase hexadecimal string prefixed with “0x”, for exa ...

  3. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  4. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  5. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  6. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  7. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  8. lambda 表达式+python内置函数

    #函数 def f1(a,b): retrun  a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...

  9. 学习过程中遇到的python内置函数,后续遇到会继续补充进去

    1.python内置函数isinstance(数字,数字类型),判断一个数字的数字类型(int,float,comple).是,返回True,否,返回False2.python内置函数id()可以查看 ...

随机推荐

  1. 接触vsto,开发word插件的利器

    研究word插件有一段时间了,现在该是总结的时候了. 首先咱们来了解下什么是vsto?所谓vsto,就是vs面向office提供的一个开发平台.一个开发平台至少包含两个要素:开发工具(sdk)和运行环 ...

  2. WPF自学入门(七)WPF 初识Binding

    今天记录一下Binding的基础和具体的使用方法,说起这个Binding,在WPF中,Binding是很重要的特征,在传统的Windows软件来看,大多数都是UI驱动程序的模式,也可以说事件驱动程序, ...

  3. Redis 基础(一)

    Remote Dictionary Server(Redis)是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI C语言编写.遵守BSD ...

  4. js实现二叉树

    //binary tree//add order remove findfunction tree() { var node = function(key) {  this.left = null;  ...

  5. git pull error

    在图形界面中,执行拉取操作时,出现下面的错误. You asked to pull from the remote 'origin', but did not specifya branch. Bec ...

  6. ABAP调试

    ABAP 开发系列(02): ABAP Development Workbench 介绍(下)- ABAP 调试器 8. Debugger – ABAP 调试器 开发程序,调试器是必不可少的工具,而A ...

  7. Filecoin2017年Q4进度更新(完整版)

    亲爱的Filecoin支持者.矿工.用户.投资者和广大的社区朋友们, 自从Token销售完成以后,我们便开始集中精力把Filecoin项目从设想变为现实-从实现Filecoin协议的核心代码到打造我们 ...

  8. python 一篇就能理解函数基础

    一,函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过 ...

  9. mysql授权报错

    mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '<zabbix>';ERROR ...

  10. RESTful WebService 入门实例

      /* 新建MavenProject,使用以下代码,创建类和POM文件.使用命令行切换到Project根目录,运行mvn package(或者,选中pom.xml 文件右键单击 > run a ...