如果没有明确指定某种类型,则使用
type内置函数在运行时发现答案没有任何问题:
>>> import re>>> retype = type(re.compile('hello, world'))>>> isinstance(re.compile('goodbye'), retype)True>>> isinstance(12, retype)False>>>在运行时发现类型可以防止您访问私有属性以及将来对返回类型的更改。在
type这里使用并没有什么不好的地方,尽管根本不想知道类型。

如果没有明确指定某种类型,则使用
type内置函数在运行时发现答案没有任何问题:
>>> import re>>> retype = type(re.compile('hello, world'))>>> isinstance(re.compile('goodbye'), retype)True>>> isinstance(12, retype)False>>>在运行时发现类型可以防止您访问私有属性以及将来对返回类型的更改。在
type这里使用并没有什么不好的地方,尽管根本不想知道类型。