在哪里“导入”?

编程入门 行业动态 更新时间:2024-10-25 01:36:25
本文介绍了在哪里“导入”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨! 如果我有两个文件.py如 m.py 来自c进口* ... x = c() ... os.any_method .. 。 ... c.py c级: def __init __(自我,...): ... os.any_method ... ... ... 使用os模块我应该在哪里放import os?在这两个文件中? 谢谢 Paulo

解决方案

" Paulo da Silva" < ps ******** @ esotericaX.ptXwrote in message news:11 *************** @ iceman.esoterica.pt。 .. |嗨! | |如果我有两个文件.py如 | | m.py |来自c import * | ...... | x = c() | ...... | os.any_method ... | ... | | c.py | c级: | def __init __(self,...): | ...... | os.any_method ... | ...... | ... | |使用os模块我应该在哪里放import os?在这两个文件中? 即使在m.py中使用它也不是*目前*必要。将os导入到m.py中取决于你编写的方式和导入c.py 看起来很脆弱,记住c肯定是一个内存负担 所以。另外,我可能会改为使用''import c'。 tjr

Paulo da Silva a écrit:

嗨! 如果我有两个文件.py如 m.py 来自c import *

避免这种导入,除非是交互式口译员和 最终在一个包__init__.py中。最好使用: 来自c import c 或 import c .. 。 x = cc()

... x = c() ... os.any_method ...

然后你需要导入os

... c.py class c:

class C(object): 1 /更好地坚持命名约定(CamelCase中的类名) 2 /帮个忙:使用新式课程

def __init __(self,...): ... os .any_method ... ... ... 都使用os模块我应该把导入操作系统?在这两个文件中?

是的。在上面的示例中,由于from c import *,它起作用了。 - 这正是为什么在一个模块中使用它的好形式(好吧: '这是其中一个原因)。每个模块应*明确*导入 所有它的直接依赖。

都使用os模块我应该放在哪里import os?在这两个文件中? 你真的没有选择,你必须在两个文件中导入操作系统。 Python只会加载os模块进入内存一次,但需要导入 语句将os模块添加到c和m模块 命名空间。 c.py中的代码不能看到m.py中的代码,即使它是由m.py导入的。导入命令与C中的#include不同,它是不是代码转储。 -m

Hi! If I have two files .py such as m.py from c import * ... x=c() ... os.any_method ... ... c.py class c: def __init__(self, ...): ... os.any_method ... ... ... both using os module where should I put the "import os"? In both files? Thanks Paulo

解决方案

"Paulo da Silva" <ps********@esotericaX.ptXwrote in message news:11***************@iceman.esoterica.pt... | Hi! | | If I have two files .py such as | | m.py | from c import * | ... | x=c() | ... | os.any_method ... | ... | | c.py | class c: | def __init__(self, ...): | ... | os.any_method ... | ... | ... | | both using os module where should I put the "import os"? In both files? I would even though having it in m.py is not *currently* necessary. Having the import of os into m.py depend on the way you write and import c.py looks fragile, and it certainly is a memory burden to remember that c does so. Also, I probably would use ''import c'' instead. tjr

Paulo da Silva a écrit :

Hi! If I have two files .py such as m.py from c import *

avoid this kind of import except in an interactive interpreter and eventually in a package __init__.py. Better to use either: from c import c or import c ... x = c.c()

... x=c() ... os.any_method ...

Then you need to import os

... c.py class c:

class C(object): 1/ better to stick to naming conventions (class names in CamelCase) 2/ do yourself a favor: use new-style classes

def __init__(self, ...): ... os.any_method ... ... ... both using os module where should I put the "import os"? In both files?

Yes. In your above example, it worked because of the "from c import *" - and this is exactly why it''s bad form to use this in a module (well: that''s one of the reasons why). Each module should *explicitly* import all it''s direct dependencies.

both using os module where should I put the "import os"? In both files? You don''t really have a choice, you have to import os in both files. Python will only load the os module into memory once, but the import statements are needed to add the os module to the c and m module namespaces. The code in c.py cannot ''see'' the code in m.py, even if it was imported by m.py. The import command is not like #include in C, it is not a code dump. -m

更多推荐

在哪里“导入”?

本文发布于:2023-11-08 06:56:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!