打字.任何 vs 对象? [英] typing.Any vs object?
问题描述
在打字时使用 typing.Any
和 object
有什么区别吗?例如:
def get_item(L: list, i: int) ->打字.任何:返回 L[i]
相比:
def get_item(L: list, i: int) ->目的:返回 L[i]
是的,有区别.尽管在 Python 3 中,所有对象都是 object
的实例,包括 object
本身,但只有 Any
文档表明类型检查器应该忽略返回值.
Any
类型的文档字符串声明对象是 Any
的子类,反之亦然:
然而,一个合适的类型检查器(一个超越了isinstance()
检查,并检查对象在函数中实际使用的方式)可以很容易地反对object
其中 Any
总是被接受.
来自 Any
类型文档:
请注意,将 Any
类型的值分配给更精确的类型时,不会执行类型检查.
和
<块引用>将Any
的行为与object
的行为进行对比.与Any
类似,每个类型都是object
的子类型.然而,与 Any
不同的是,反之则不然:对象不是所有其他类型的子类型.
这意味着当一个值的类型是 object
时,类型检查器将拒绝几乎所有对其进行的操作,并将其分配给一个更复杂的变量(或将其用作返回值)特殊类型是类型错误.
以及来自 mypy 文档部分的Any vs.对象:
<块引用>类型 object
是另一种可以将任意类型的实例作为值的类型.与Any
不同,object
是一个普通的静态类型(类似于Java中的Object
),只接受对所有类型有效的操作对象值.
object
可以 cast 到一种更具体的类型,而 Any
真正意味着任何事情,并且类型检查器不会对该对象的任何使用(即使您稍后将这样的对象分配给一个名称是类型检查的).
您已经通过接受 list
将函数绘制到一个无类型的角落,这归结为与 List[Any]
相同.类型检查器在那里脱离并且返回值不再重要,但是由于您的函数接受包含 Any
对象的列表,因此正确的返回值将是 Any
在这里.
要正确参与类型检查代码,您需要将输入标记为 List[T]
(一个通用类型的容器),以便类型检查器能够关心返回值.在您的情况下将是 T
,因为您正在从列表中检索一个值.从 TypeVar
创建 T
:
from typing import TypeVar, ListT = TypeVar('T')def get_item(L: List[T], i: int) ->:返回 L[i]
Is there any difference between using typing.Any
as opposed to object
in typing? For example:
def get_item(L: list, i: int) -> typing.Any:
return L[i]
Compared to:
def get_item(L: list, i: int) -> object:
return L[i]
Yes, there is a difference. Although in Python 3, all objects are instances of object
, including object
itself, only Any
documents that the return value should be disregarded by the typechecker.
The Any
type docstring states that object is a subclass of Any
and vice-versa:
>>> import typing
>>> print(typing.Any.__doc__)
Special type indicating an unconstrained type.
- Any object is an instance of Any.
- Any class is a subclass of Any.
- As a special case, Any and object are subclasses of each other.
However, a proper typechecker (one that goes beyond isinstance()
checks, and which inspects how the object is actually used in the function) can readily object to object
where Any
is always accepted.
From the Any
type documentation:
Notice that no typechecking is performed when assigning a value of type
Any
to a more precise type.
and
Contrast the behavior of
Any
with the behavior ofobject
. Similar toAny
, every type is a subtype ofobject
. However, unlikeAny
, the reverse is not true: object is not a subtype of every other type.That means when the type of a value is
object
, a type checker will reject almost all operations on it, and assigning it to a variable (or using it as a return value) of a more specialized type is a type error.
and from the mypy documentation section Any vs. object:
The type
object
is another type that can have an instance of arbitrary type as a value. UnlikeAny
,object
is an ordinary static type (it is similar toObject
in Java), and only operations valid for all types are accepted for object values.
object
can be cast to a more specific type, while Any
really means anything goes and a type checker disengages from any use of the object (even if you later assign such an object to a name that is typechecked).
You already painted your function into a an un-typed corner by accepting list
, which comes down to being the same thing as List[Any]
. The typechecker disengaged there and the return value no longer matters, but since your function accepts a list containing Any
objects, the proper return value would be Any
here.
To properly participate in type-checked code, you need to mark your input as List[T]
(a genericly typed container) for a typechecker to then be able to care about the return value. Which in your case would be T
since you are retrieving a value from the list. Create T
from a TypeVar
:
from typing import TypeVar, List
T = TypeVar('T')
def get_item(L: List[T], i: int) -> T:
return L[i]
这篇关于打字.任何 vs 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!