Python built-in functions (46) -- oct, python built-in 46oct

Source: Internet
Author: User

Python built-in functions (46) -- oct, python built-in 46oct

English document:

oct( X)
Convert an integer number to an octal string. The result is a valid Python expression. If XIs not a Python intObject, it has to define __index__()Method 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 () # The returned result type is a string <class 'str' >>>> oct (10.0) # floating point numbers cannot be converted to octal Traceback (most recent call last ): file "<pyshell #3>", line 1, in <module> oct (10.0) TypeError: 'float' object cannot be interpreted as an integer >>> oct ('10') # strings cannot be converted to octal Traceback (most recent call last ): file "<pyshell #4>", line 1, in <module> 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.

# Undefined _ index _ function, which cannot be converted >>> class Student: def _ init _ (self, name, age): self. name = name self. age = age >>> a = Student ('Kim ', 10) >>> oct (a) Traceback (most recent call last): File "<pyshell #12> ", line 1, in <module> oct (a) TypeError: 'student 'object cannot be interpreted as an integer # defines the _ index _ function, but the returned value is not of the int type, 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 "<pyshell #18> ", line 1, in <module> oct (a) TypeError: _ index _ returned non-int (type str) # defines the _ index _ function, the return value is of the int type and can be converted to >>> 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'

 

Related Article

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.