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
int
Object, 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'