python convert函数_Python内置函数
英文文档:
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内置函数相关推荐
- decode函数_Python 内置函数总一
内置函数 python内置函数,截至python3.6.2版本之前一共68个内置函数,内置函数是可以直接使用的函数. Python标准库/内置函数链接:点击此处 内置函数分类 作用域相关 基于字典的形 ...
- python内置方法就是内置函数_python内置函数
什么是内置函数? 就是python给你提供的,拿来直接用的函数, 比如print 和 input等等. 截止到python版本3.6.2 python一共提供了68个内置函数. 他们就是python直 ...
- python filter内置函数_python 内置函数filter
python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...
- python内置的数字运算函数_Python 内置函数1
abs(x)函数 返回绝对值 参数可以是:负数.正数.浮点数或者长整形 print(abs(-1.2)) # 结果1.2 cmp(x, y)函数 (python3已删) 中文说明: 比较两个对象x和y ...
- python property函数_Python内置函数property()如何使用
代码 class Shuxing(): def __init__(self, size = 10): self.size = size def getSize(self): print('getSiz ...
- exec是不是python的内置函数_Python内置函数(62)——exec
英文文档: exec(object[, globals[, locals]])This function supports dynamic execution of Python code. obje ...
- python compile函数_python 内置函数 compile()
python 内置函数 compile() 这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. ...
- python 乘法内置函数_Python内置函数--reversed()
python内置了一些非常精巧且强大的函数,对初学者来说,一般不怎么用到,但是偶尔会碰到,我也是用了一段时间python之后才发现,哇还有这么好的函数,每个函数都非常经典,而且经过严格测试,使用内置函 ...
- python filter函数_python 内置函数filter
python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...
- python的globals函数_Python内置函数之globals()与locals()
Python内置函数globals() 函数与locals()函数简介. 在Python中,函数拥有自己的作用域,或者称为命名空间.在函数内访问某个变量会优先在函数自己的命名空间中进行访问. 而要想知 ...
最新文章
- centos7安装mysql .gz_转:centos7安装mysql.tar.gz
- 字符串的UPDATE和REPLACE部分
- topcoder srm 325 div1
- 利用JNative实现Java调用动态库
- 图像识别:微信跳一跳机器人
- java日历类add方法_Java日历setFirstDayOfWeek()方法与示例
- React的组件模式 1
- ubuntu man命令彩色高亮显示
- appium部分操作
- 写博客是一种乐趣,一种需要培养的乐趣,Java程序员最大的悲哀是什么
- Flutter高级第7篇:点击穿透问题、页面禁止左右滑动
- 线下见面会,下一站定啦!
- xampp mysql使用教程_nodejs使用xampp中的mysql教程
- ps一点等于多少厘米_请问PS中“像素”和“厘米”是怎么换算的?
- Android蓝牙开发与串口蓝牙通讯
- 修改图片exif信息
- 夏令时-前端要知道的知识
- tensorflow代码翻译成pytorch代码 -详细教程+案例
- 直方图归一化因子计算公式
- Java获取图片的拍摄地理位置
热门文章
- halcon求取区域顶点
- HALCON示例程序count_pellets.hdev分割豆子,基本形态学的使用
- HALCON示例程序bottle.hdev、bottlet.hdev瓶体字符OCR的训练和检测
- 学习笔记(21):Python网络编程并发编程-队列的使用
- 学习笔记(20):Python网络编程并发编程-互斥锁与join的区别
- 【数字信号处理】 第二章、时域中的离散时间信号
- python股票分析系统_熬了一晚上,小白用Python写了一个股票提醒系统
- java字符串排序_对字符串排序持一种宽容的心态
- 土压力图怎么画_电气系统图怎么画?
- iOS扩大按钮的点击范围