OCT of Python built-in functions

Source: Internet
Author: User
This article introduces the built-in function oct () in python, which is used to convert the decimal number to octal and then to the character. If you have any need, refer to the following English documents:

oct ( x )Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Pythonobject, it has to define anmethod that returns an integer.

Note:

1. the Function converts an integer to an octal string. If a floating point or string is input, an error is returned.

>>> A = oct (10) >>> a '0o12' >>> type (a) # The type of the returned result is a string.
 
  
>>> Oct (10.0) # floating point numbers cannot be converted to octal Traceback (most recent call last): File"
  
   
", Line 1, in
   
    
Oct (10.0) TypeError: 'float' object cannot be interpreted as an integer >>> oct ('10') # The string cannot be converted to an octal Traceback (most recent call last): File"
    
     
", Line 1, in
     
      
Oct ('10') TypeError: 'str' object cannot be interpreted as an integer
     
    
   
  
 

2. if the input parameter is not an integer, it must be an instance object of the class that defines _ index _ and returns the integer function.

# The _ index _ function cannot be converted.

>>> Class Student:

Def _ init _ (self, name, age ):

Self. name = name

Self. age = age

>>> A = Student ('Kim ', 10)

>>> Oct ()

Traceback (most recent call last ):

File" ", Line 1, in

Oct ()

TypeError: 'Student 'object cannot be interpreted as an integer

# The _ index _ function is defined, but the return value is not of the int type and cannot be converted.

>>> Class Student: def _ init _ (self, name, age): self. name = name self. age = age def _ index _ (self): return self. name >>> a = Student ('Kim ', 10) >>> oct (a) Traceback (most recent call last): File"
 
  
", Line 1, in
  
   
Oct (a) TypeError: _ index _ returned non-int (type str) # defines the _ index _ function, and the return value is of the int type, conversion> class Student: def _ init _ (self, name, age): self. name = name self. age = age def _ index _ (self): return self. age >>>> a = Student ('Kim ', 10) >>> oct (a) '0o12'
  
 

The above is a detailed explanation of the Python built-in function OCT. For more articles, please follow the PHP Chinese network (www.php1.cn )!

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.