Python built-in hex function

Source: Internet
Author: User
This article provides a detailed description of the Python built-in hex function in English:

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.

Note:

1. the Function converts a 10-digit integer to a 16-digit integer.

>>> hex(15)'0xf'>>> hex(16)'0x10'

2. if The x parameter 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 ', 10) >>> hex (s) Traceback (most recent call last): File"
 
  
", Line 1, in
  
   
Hex (s) TypeError: 'Student 'object cannot be interpreted as an integer # defines the _ index _ function, but returns a string >>> 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) # defines the _ index _ function and returns an integer >>> 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'
    
   
  
 

The above is a detailed description of the Python built-in hex function. For more information, see other related articles in the first PHP community!

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.