英文文档:

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

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

    英文文档: hex(x) Convert an integer number to a lowercase hexadecimal string prefixed with "0x" ...

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

    英文文档: super([type[, object-or-type]]) Return a proxy object that delegates method calls to a parent ...

  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内置函数简单归纳

    做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...

  8. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  9. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

随机推荐

  1. python基础day3

    一.文件管理 文件管理是很多应用程序的基本功能和重要组成部分.Python可以使文件管理极其简单,特别是和其它语言相对比. 1.    读操作 1.1r模式 1.1.1读取其他路径下文件 首先在D盘创 ...

  2. Weblogic记录

    有些坑还是要去踩,上来就docker一脸懵逼. 1.应用 https://www.cnblogs.com/xdp-gacl/p/4140683.html (1)安装 环境: 64位server2016 ...

  3. C++使用 jsoncpp 解析json数据

    整合自网路 一.安装的方法 1.安装 scons 下载地址:http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.ta ...

  4. python程序入门 基础教程

    1.VSCode基础使用+VSCode调试python程序入门 2.pip 安装 3.scrapy安装 4.python解析xml

  5. 免费获取SSL证书/一键安装SSL证书/https加密

    因为我用的是恒创的香港服务器 虽然价格相较于大促的阿里云贵一些,但是有一个有点不用备案... 安装步骤: 1.登录云主机控制面板, 在 其他管理 中找到并进入 SSL证书 设置. 注意:如拥有多个域名 ...

  6. 在dcef3当中执行js代码并获得返回值

    1.如何在dcef3当中执行js代码 procedure TForm1.btnWriteZMClick(Sender: TObject);var  js: string;begin  js := 'd ...

  7. C#三目运算符

    在编写项目的时候,会经常用到 if else 判断语句,但有些简单的判断或赋值,可以通过三目运算符来完成! 例如: int sex=0; string sexText=""; if ...

  8. vue-router下的html5 history在iis服务器上的设置 vue去掉#

    转自:https://www.cnblogs.com/zzsdream/p/6576639.html 1.安装 url rewrite模块到IIS 下载地址 2.在web.config文件中 syst ...

  9. 元组Tuple的使用

    在方法中有多个值返回,返回值封装成对象又不方便,可以用 out 返回或ref返回, 这里介绍元组,也可以作为多个返回值的使用,最多携带8个返回值 Task.Factory.StartNew<Tu ...

  10. ubuntu Anaconda install

    在文件目录下执行: bash Anaconda3-4.2.0-Linux-x86_64.sh 根据提示输入回车 这里需要查看注册信息,回车浏览完信息即可 阅读完注册信息后,这里输入“yes” 回车即可 ...