使用python类型提示时,Text vs str
当类型注释字符串、文本或str时应该使用什么。 使用两者的区别是什么 例如:使用python类型提示时,Text vs str,python,python-3.x,string,annotations,python-typing,Python,Python 3.x,String,Annotations,Python Typing,当类型注释字符串、文本或str时应该使用什么。 使用两者的区别是什么 例如: from typing import Text def spring(a: Text) -> Text: return a.upper() 或 从文件中(如Ians评论中所述): Text是str的别名。提供它是为了为Python 2代码提供向前兼容的路径:在Python 2中,Text是unicode的别名 使用文本指示值必须以与Python 2和Python 3兼容的方式包含unicode字符串:
from typing import Text
def spring(a: Text) -> Text:
return a.upper()
或
从文件中(如Ians评论中所述):
Text
是str
的别名。提供它是为了为Python 2代码提供向前兼容的路径:在Python 2中,Text
是unicode
的别名
使用文本指示值必须以与Python 2和Python 3兼容的方式包含unicode字符串:
def add_unicode_checkmark(text:text)->text:
返回文本+u'\u2713'
文档解释得很好。。。哦。这意味着对于python3,我可以使用str,而不会出现任何问题@iainshelvington这意味着对于python3,它们是等价的,没有明显的区别得到了,谢谢@Iain
def spring(a: str) -> str:
return a.upper()