在python中键入模块的联合会做什么?

发布于 2025-01-24 06:14:06 字数 914 浏览 0 评论 0原文

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 BlockBottleNeck是第167行之前定义的两个类当我查找类型Union时或瓶颈block本身已传递给名为_make_layer的函数,该函数在第223行中定义,是resnet类的方法,我想知道>如何_make_layer知道传递的参数是basic basic basic basic>还是bottleneck

当某人制作resnet类的实例时,他们是否应该通过basicblockBottleNeck的对象?这是_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2025-01-31 06:14:06

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.

非要怀念 2025-01-31 06:14:06

加强先前的答案。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文