英文文档:

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. git本地项目关联远程仓库

    应用场景: 当你在开发一个项目的时候,不想只在本地存储,想用git来管理代码时候的. 1.在你的项目根目录打开git命令窗口,通过 git init 命令把这个目录变成Git可以管理的仓库: git ...

  2. openflow 和 sdn (软件定义网络) 原理和教程

    OpenFlow概述 OpenFlow简介 通俗的讲,OpenFlow是使用类似于API进程配置网络交换机的协议.OpenFlow的思路很简单,网络设备维护一个FlowTable并且只按照FlowTa ...

  3. ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to chec

    数据库连接超时,是数据库连接时的相关配置写错,例如:数据库密码,驱动等问题

  4. NGUI_Texture

    六.UITexture:在屏幕上显示一张图片,和Sprite类似,但是UITexture会单独消耗一个DrawCall去渲染, 会单独加载进内存,会增加内存的开销. 1.使用UITexture时要遵循 ...

  5. SQL server 2008 安装提示:属性不匹配

    问题 安装SQL server 2008提示属性不匹配 解决方案 确保C盘以及其子文件夹C:\Program Files\Microsoft SQL Server和C:\Program Files ( ...

  6. 笔记:Spring Cloud Eureka 服务治理

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  7. MySQL数据库学习二 MSQL安装和配置

    2.1 下载和安装MySQL软件 2.1.1 基于客户端/服务器(C/S)的数据库管理系统 服务器:MySQL数据库管理系统 客户端:操作MySQL服务器 2.1.2 MySQL的各种版本 社区版(C ...

  8. sharepoint REST API 获取文件夹及文件

    使用REST操作文件夹: 获取文件夹 url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')/f ...

  9. 15分钟理解HTTPS——通俗篇

    | 导语 它很深奥吗?你肯定常常见过它,使用它,甚至离不开它... 它很浅显吗?你可能觉得看透它,理解它,甚至懂它... 让我们用15分钟,不那么学术地将它的深挖到底~ 什么?如何证明我是我?本文要上 ...

  10. [mysql] MySQL解压缩安装步骤

    以前装的MySQL出问题了,只好卸载了. 又下载了一个mysql-5.6.24-win32.1432006610.zip.msi文件直接安装就行了.这里需要解压到指定目录,配置后可使用. 环境变量配置 ...