文本 vs str,使用 python 类型提示时
Text vs str, when using python type hints
在键入注释字符串、Text 或 str 时应该使用什么?
使用两者有什么区别?
例如:
from typing import Text
def spring(a: Text) -> Text:
return a.upper()
或
def spring(a: str) -> str:
return a.upper()
来自文档(如 Ians 评论中所述):
Text
is an alias for str
. It is provided to supply a forward compatible path for Python 2 code: in Python 2, Text
is an alias for unicode
.
Use Text to indicate that a value must contain a unicode string in a manner that is compatible with both Python 2 and Python 3:
def add_unicode_checkmark(text: Text) -> Text:
return text + u' \u2713'
在键入注释字符串、Text 或 str 时应该使用什么? 使用两者有什么区别?
例如:
from typing import Text
def spring(a: Text) -> Text:
return a.upper()
或
def spring(a: str) -> str:
return a.upper()
来自文档(如 Ians 评论中所述):
Text
is an alias forstr
. It is provided to supply a forward compatible path for Python 2 code: in Python 2,Text
is an alias forunicode
.Use Text to indicate that a value must contain a unicode string in a manner that is compatible with both Python 2 and Python 3:
def add_unicode_checkmark(text: Text) -> Text: return text + u' \u2713'