A detailed introduction to Python's built-in hex function

Source: Internet
Author: User
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 ' 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.