在python中键入模块的联合会做什么?
I was looking the implementation of ResNet deep learning architecture in PyTorch from git-hub 。 在第167行中,在定义Resnet的另一个类定义的初始化器中,也称为Resnet,我看到了下面的代码:
block: Type[Union[BasicBlock, Bottleneck]],
Basic Blasic Block
和BottleNeck
是第167行之前定义的两个类当我查找类型
和Union
时或瓶颈
。 block
本身已传递给名为_make_layer
的函数,该函数在第223行中定义,是resnet
类的方法,我想知道>如何
_make_layer
知道传递的参数是basic basic basic basic>
还是bottleneck
?
当某人制作resnet
类的实例时,他们是否应该通过basicblock
或BottleNeck
的对象?这是_make_layer
知道它的参数吗?那为什么我们需要使用Union
?
I was looking the implementation of ResNet deep learning architecture in PyTorch from git-hub.
At line 167, inside the initializer of another class definition which defines ResNet and is also named ResNet, I saw the code below:
block: Type[Union[BasicBlock, Bottleneck]],
BasicBlock
and Bottleneck
are two classes defined before line 167. When I looked up for what Type
and Union
do, as far as I understood it says that block
could be either of BasicBlock
or Bottleneck
. block
itself was passed to a function named _make_layer
which is defined at line 223 and is a method of ResNet
class and I wonder how _make_layer
know how the passed argument is either BasicBlock
or Bottleneck
?
When someone is making an instance of ResNet
class, should they pass an object of BasicBlock
or Bottleneck
? and is this how _make_layer
knows what it got as its argument? Then why do we need to use Union
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Union
在这种情况下,意味着允许两种类型。请记住,在Python中打字并未强制执行。仅检查。因此,您仍然可以通过您想要的任何东西,并且它可以通过工作。但是,像Pyright或Mypy这样的类型检查器会提醒您差异。
Union
in this case means both types are allowed.Remember typing in python isn't enforced. It's only checked. So you can still pass whatever you want and it might work. However, a type checker like pyright or mypy will alert you to the discrepancy.
加强先前的答案。 Python不太在乎类型。他们被完全忽略了。他们的唯一目的是用于衬里。
IDE就像Pycharm一样,其中还内置了衬里。 Pycharm为我捕获了许多错误:“您说该功能应该拿出两个字符串,但您将其传递给整数和一个字符串”。 Python本身并不在乎。
To reinforce the previous answer. Python couldn't care less about types. They are ignored completely. Their sole purpose is for linters.
IDE's like PyCharm also have linters built into them. PyCharm has caught numerous bugs for me: "you said that function was supposed to take a two strings, but you're passing it an integer and a string". Python itself just doesn't care.