英文文档:

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 "", line 1, in 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 "", line 1, in 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'

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

python convert函数_Python内置函数相关推荐

  1. decode函数_Python 内置函数总一

    内置函数 python内置函数,截至python3.6.2版本之前一共68个内置函数,内置函数是可以直接使用的函数. Python标准库/内置函数链接:点击此处 内置函数分类 作用域相关 基于字典的形 ...

  2. python内置方法就是内置函数_python内置函数

    什么是内置函数? 就是python给你提供的,拿来直接用的函数, 比如print 和 input等等. 截止到python版本3.6.2 python一共提供了68个内置函数. 他们就是python直 ...

  3. python filter内置函数_python 内置函数filter

    python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...

  4. python内置的数字运算函数_Python 内置函数1

    abs(x)函数 返回绝对值 参数可以是:负数.正数.浮点数或者长整形 print(abs(-1.2)) # 结果1.2 cmp(x, y)函数 (python3已删) 中文说明: 比较两个对象x和y ...

  5. python property函数_Python内置函数property()如何使用

    代码 class Shuxing(): def __init__(self, size = 10): self.size = size def getSize(self): print('getSiz ...

  6. exec是不是python的内置函数_Python内置函数(62)——exec

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

  7. python compile函数_python 内置函数 compile()

    python 内置函数 compile() 这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. ...

  8. python 乘法内置函数_Python内置函数--reversed()

    python内置了一些非常精巧且强大的函数,对初学者来说,一般不怎么用到,但是偶尔会碰到,我也是用了一段时间python之后才发现,哇还有这么好的函数,每个函数都非常经典,而且经过严格测试,使用内置函 ...

  9. python filter函数_python 内置函数filter

    python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...

  10. python的globals函数_Python内置函数之globals()与locals()

    Python内置函数globals() 函数与locals()函数简介. 在Python中,函数拥有自己的作用域,或者称为命名空间.在函数内访问某个变量会优先在函数自己的命名空间中进行访问. 而要想知 ...

最新文章

  1. centos7安装mysql .gz_转:centos7安装mysql.tar.gz
  2. 字符串的UPDATE和REPLACE部分
  3. topcoder srm 325 div1
  4. 利用JNative实现Java调用动态库
  5. 图像识别:微信跳一跳机器人
  6. java日历类add方法_Java日历setFirstDayOfWeek()方法与示例
  7. React的组件模式 1
  8. ubuntu man命令彩色高亮显示
  9. appium部分操作
  10. 写博客是一种乐趣,一种需要培养的乐趣,Java程序员最大的悲哀是什么
  11. Flutter高级第7篇:点击穿透问题、页面禁止左右滑动
  12. 线下见面会,下一站定啦!
  13. xampp mysql使用教程_nodejs使用xampp中的mysql教程
  14. ps一点等于多少厘米_请问PS中“像素”和“厘米”是怎么换算的?
  15. Android蓝牙开发与串口蓝牙通讯
  16. 修改图片exif信息
  17. 夏令时-前端要知道的知识
  18. tensorflow代码翻译成pytorch代码 -详细教程+案例
  19. 直方图归一化因子计算公式
  20. Java获取图片的拍摄地理位置

热门文章

  1. halcon求取区域顶点
  2. HALCON示例程序count_pellets.hdev分割豆子,基本形态学的使用
  3. HALCON示例程序bottle.hdev、bottlet.hdev瓶体字符OCR的训练和检测
  4. 学习笔记(21):Python网络编程并发编程-队列的使用
  5. 学习笔记(20):Python网络编程并发编程-互斥锁与join的区别
  6. 【数字信号处理】 第二章、时域中的离散时间信号
  7. python股票分析系统_熬了一晚上,小白用Python写了一个股票提醒系统
  8. java字符串排序_对字符串排序持一种宽容的心态
  9. 土压力图怎么画_电气系统图怎么画?
  10. iOS扩大按钮的点击范围