Python中“from ... import ...”语法背后的推理 [英] Reasoning behind `from ... import ...` syntax in Python

查看:160
本文介绍了Python中“from ... import ...”语法背后的推理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是想知道为什么从模块导入特定对象的语法是来自模块import x,y,z 而不是 import x,y ,z来自模块。我不是母语,但后者不是更正确/自然吗?

I always wondered why the syntax for importing specific objects from a module is from module import x, y, z instead of import x, y, z from module. I'm not a native speaker, but isn't the latter more correct/natural?

那么,从第一个开始的原因是什么?它只是简化语法(需要更少的前瞻)?是否试图使这两种进口在视觉上更加鲜明?或者这是其中一种明显的方式是除非你是荷兰人,否则一开始并不明显? ;)

So, what is the reason to put the from first? Is it merely to simplify the grammar (require less lookahead)? Is it an attempt to make the two kinds of imports visually more distinct? Or is it one of these cases where the obvious way is "not obvious at first unless you're Dutch"? ;)

推荐答案

不知道为什么实际这样做了,但这就是我的方式做到这一点,仅仅因为,作为一种工程类型,从一般类别开始并向下钻取细节似乎更自然。

No idea why it was actually done that way but it's the way I'd do it, simply because, being an engineering type, it seems more natural to me to start from a general category and drill down to specifics.

这也意味着解析器如果顺序处理,则必须存储更少的东西。使用:

It would also mean the parser would have to store less stuff if processing sequentially. With:

import x,y,z from a

你必须记住 x y ž。使用:

you have to remember x, y and z. With:

from a import x,y,z

你只需要记住 a

这就是为什么当我第一次遇到Perl的post-if变体时遇到这么多麻烦的原因:

That's why I had so much trouble when I first encountered Perl's post-if variant:

$x = $y if $y > 40;

因为你事先不知道 你正在阅读的是什么是否有条件。

since you don't know in advance whether what you're reading is conditional or not.

这篇关于Python中“from ... import ...”语法背后的推理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆