English documents:
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 have to define an index () method, which returns an integer.
Description
1. function function converts 10 binary integers to 16 binary integers.
>>> hex ' 0xf ' >>> hex (+) ' 0x10 '
2. If the parameter x is not an integer, it must define an index function that returns an integer.
# undefined __index__ function >>> class Student:def __init__ (self,name,age): Self.name = Name Self.age = age>>> >>> s = Student (' Kim ', ten) >>> hex (s) Traceback (most R Ecent call last): File "<pyshell#17>", line 1, in <module> Hex (s) TypeError: ' Student ' object cannot is int Erpreted as an integer# defines the __index__ function, but returns the String >>> class Student:def __init__ (self,name,age): Self.name = Name Self.age = Age def __index__ (self): return self.name>>> s = Student (' Kim ', ten) >>> Hex (s) Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> Hex (s) TypeError: __inde X__ returned Non-int (type str) # defines the __index__ function and returns the integer >>> class student:def __init__ (self,name,age): Sel F.name = Name Self.age = Age def __index__ (self): return self.age>>> s = Student (' Kim ', ten) >&G T;> Hex (s) ' 0xa '