gpt4 book ai didi

python - Python 3.6+ 中是否有格式化的字节字符串文字?

转载 作者:行者123 更新时间:2023-12-02 06:32:23 29 4
gpt4 key购买 nike

我正在寻找格式化的字节字符串文字。具体来说,相当于

name = "Hello"
bytes(f"Some format string {name}")

可能类似于fb“某些格式字符串{name}”

这样的事情存在吗?

最佳答案

没有。 The idea is explicitly dismissed in the PEP :

For the same reason that we don't support bytes.format(), you may not combine 'f' with 'b' string literals. The primary problem is that an object's __format__() method may return Unicode data that is not compatible with a bytes string.

Binary f-strings would first require a solution for bytes.format(). This idea has been proposed in the past, most recently in PEP 461. The discussions of such a feature usually suggest either

  • adding a method such as __bformat__() so an object can control how it is converted to bytes, or

  • having bytes.format() not be as general purpose or extensible as str.format().

Both of these remain as options in the future, if such functionality is desired.

关于python - Python 3.6+ 中是否有格式化的字节字符串文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60969698/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com