在哪里“导入”? [英] Where to "import"?

查看:85
本文介绍了在哪里“导入”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


如果我有两个文件.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


这篇关于在哪里“导入”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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