python 并发
Python bin() function is used to convert an integer into the binary format string. The formatted string is prefixed with “0b”.
Python bin()函数用于将整数转换为二进制格式的字符串。 格式化的字符串以“ 0b”为前缀。
Python bin() (Python bin())
Python bin() function can be used with integers having different formats such as octal, hexadecimal too. The function will take care of converting them into the binary string. Let’s look at some examples of bin() function.
Python bin()函数也可用于具有不同格式的整数,例如八进制,十六进制。 该函数将负责将它们转换为二进制字符串。 让我们看一下bin()函数的一些示例。
x = 10
y = bin(x)
print(type(y))
print(bin(x))
Output:
输出:
<class 'str'>
0b1010
From the output, it’s clear that the bin() function returns a string and not a number. Python type() function returns the type of the object.
从输出中很明显,bin()函数返回的是字符串而不是数字。 Python type()函数返回对象的类型。
带有其他格式整数的Python bin()示例 (Python bin() example with other format integers)
Let’s see some examples of using bin() function with integers in different formats.
让我们看一些将bin()函数与不同格式的整数一起使用的示例。
x = 0b110 # 6
print(bin(x))
x = 0xF # 15
print(bin(x))
x = 0o70 # 56
print(bin(x))
Output:
输出:
0b110
0b1111
0b111000
Tip: If you don’t want “0b” prefix in the binary string, you can also use format()
function. Here is a quick example showing how to use format() function.
提示 :如果您不想在二进制字符串中添加“ 0b”前缀,则也可以使用format()
函数。 这是一个简单的示例,显示了如何使用format()函数。
x = 10
print(format(x, '#b')) # 0b1010
print(format(x, 'b')) # 1010
x= 0xF
print(format(x, 'b')) # 1111
print(f'{x:b}') # 1111 (If you knew this format, you are Python Ninja!)
Output:
输出:
0b1010
1010
1111
1111
带有float的Python bin() (Python bin() with float)
Let’s see what happens when we try to run bin() function with a float argument.
让我们看看尝试使用float参数运行bin()函数时会发生什么。
x = 10.5
print(bin(x))
Output:
输出:
TypeError: 'float' object cannot be interpreted as an integer
带有对象的Python bin() (Python bin() with Object)
If you want to have a binary string representation of an Object, then you will have to implement __index__() function that must return an integer. Let’s see this with a simple example.
如果要使用对象的二进制字符串表示形式,则必须实现__index __()函数,该函数必须返回整数。 让我们用一个简单的例子来看一下。
class Person:
id = 0
def __init__(self, i):
self.id = i
def __index__(self):
return self.id
p = Person(10)
print(bin(p))
Output: 0b1010
输出: 0b1010
If the object doesn’t define __index__() function, we will get an error message as TypeError: 'Person' object cannot be interpreted as an integer
.
如果该对象未定义__index __()函数,我们将收到错误消息TypeError: 'Person' object cannot be interpreted as an integer
。
Let’s see what happens if __index__() function returns non-int. Just change the index() function to following:
让我们看看如果__index __()函数返回non-int会发生什么。 只需将index()函数更改为以下内容:
def __index__(self):
return str(self.id)
Error: TypeError: __index__ returned non-int (type str)
错误: TypeError: __index__ returned non-int (type str)
That’s all for python bin() function to convert an integer to the binary string. We also learned that an Object can also be converted to the binary string representation by implementing __index__() function that returns an integer.
这就是python bin()函数将整数转换为二进制字符串的全部内容。 我们还了解到,通过实现返回整数的__index __()函数,也可以将Object转换为二进制字符串表示形式。
Reference: Official Documentation
参考: 官方文档
python 并发