本文目录
英文文档
hex()
小结
英文文档
hex(x)
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”.
hex()
1、函数功能将10进制整数转换成16进制整数。
>>> hex(16) '0x10' >>> hex(15) '0xf'
2、如果参数x不是整数,则它必须定义一个返回整数的__index__函数。
>>> # 未定义__index__函数 >>> class Student: def __init__(self,name,age): self.name = name self.age = age >>> s = Student('Airy', 18) >>> 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('Airy', 18) >>> 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('Airy', 18) >>> hex(s) '0x12' 小结
希望通过上面的操作能帮助大家。如果你有什么好的意见,建议,或者有不同的看法,我都希望你留言和我们进行交流、讨论。
欢迎关注微信公众号,谢谢大家支持:AiryData。
转载请注明:数据之美 Python3.6内置函数(30)——hex()
喜欢 ( 0) or 分享 (0)