从问题导入 [英] import from question
问题描述
大家好
我有三个档案:
档a1.py:
========
the_number =无
文件a2.py:
====== ==
导入a1
def init():
a1.the_number = 100
文件a3.py:
========
来自a1 import the_number
import a2
a2.init()
打印the_number,输入(the_number)
Runninr a3.py我得到:
无< type''NoneType''>
将a3.py更改为:
import a1
导入a2
a2.init()
打印a1.the_number,输入(a1.the_number)
给出:
100< type''int''>
为什么它不适用于a3的第一个版本。 py?
谢谢,
iu2
1月14日,下午4:22,iu2< isra ... @ elbit.co.ilwrote:
大家好
我有三个文件:
file a1.py:
========
the_number =无
档a2.py:
========
导入a1
def init():
a1.the_number = 100
文件a3.py:
========
来自a1 import the_number
导入a2
a2.init()
打印数字,输入(the_number)
Runninr a3.py我得到:
无< type''NoneType''>
将a3.py更改为:
导入a1
导入a2
a2.init()
打印a1.the_number,输入(a1.the_number)
给出:
100< type''int''>
为什么不呢在a3.py的第一个版本中工作?
谢谢,
iu2
试试猜猜以下代码片段打印,运行它和s如果你猜对了
:
s = {'''':无}
x = s [''a '']
s [''a''] = 1
打印x
同样的机制适用于来自... import确实如此。
HTH,
George
iu2< is ***** @ elbit.co.ilwrote:
file a3.py:
========
来自a1 import的
the_number
import a2
....
>
为什么在a3.py的第一个版本中它不起作用?
想想''import a2''是同样如下:
a2 = __import __(''a2'')
和''从a1导入the_number''大致相同as:
the_number = __import __(''a1'')。the_number
换句话说,将它们视为作业,它应该全部有意义。
Duncan Booth写道:
iu2< is **** *@elbit.co.ilwrote:
>文件a3.py:
========
从a1导入the_number
导入a2
...
>为什么它在a3.py的第一个版本中不起作用?
想想''import a2''与以下相同:
a2 = __import __(''a2'')
和''来自a1导入the_number''大致相同同样如下:
the_number = __import __(''a1'')。the_number
换句话说将它们视为作业和它应该都有意义。
这有点令人惊讶。所以来自mod import *确实将所有
标量复制到本地命名空间中的新变量中。对于我想的
对象指针也是如此,但这是透明的,因为所有副本
访问同一个对象。
我总是认为两种形式的导入是等价的,但是一种形式消除了需要明确命名空间:mod.thing
显然这个远非如此。
-
通过 http://www.teranews.com
Hi all
I''ve got three files:
file a1.py:
========
the_number = None
file a2.py:
========
import a1
def init():
a1.the_number = 100
file a3.py:
========
from a1 import the_number
import a2
a2.init()
print the_number, type(the_number)
Runninr a3.py I get:
None <type ''NoneType''>
Changing a3.py to:
import a1
import a2
a2.init()
print a1.the_number, type(a1.the_number)
gives:
100 <type ''int''>
Why doesn''t it work in the first version of a3.py?
Thanks,
iu2
On Jan 14, 4:22 pm, iu2 <isra...@elbit.co.ilwrote:Hi all
I''ve got three files:
file a1.py:
========
the_number = None
file a2.py:
========
import a1
def init():
a1.the_number = 100
file a3.py:
========
from a1 import the_number
import a2
a2.init()
print the_number, type(the_number)
Runninr a3.py I get:
None <type ''NoneType''>
Changing a3.py to:
import a1
import a2
a2.init()
print a1.the_number, type(a1.the_number)
gives:
100 <type ''int''>
Why doesn''t it work in the first version of a3.py?
Thanks,
iu2Try to guess what the following snippet prints, run it, and see if you
guessed correctly:
s = {''a'':None}
x = s[''a'']
s[''a''] = 1
print x
The same mechanism applies to what "from ... import" does.
HTH,
George
iu2 <is*****@elbit.co.ilwrote:
file a3.py:
========
from a1 import the_number
import a2
....
>
Why doesn''t it work in the first version of a3.py?
Think of ''import a2'' as being the same as:
a2 = __import__(''a2'')
and ''from a1 import the_number'' as roughly the same as:
the_number = __import__(''a1'').the_number
In other words think of them as assignments and it should all make sense.
Duncan Booth wrote:iu2 <is*****@elbit.co.ilwrote:
>file a3.py:
========
from a1 import the_number
import a2
...
>Why doesn''t it work in the first version of a3.py?
Think of ''import a2'' as being the same as:
a2 = __import__(''a2'')
and ''from a1 import the_number'' as roughly the same as:
the_number = __import__(''a1'').the_number
In other words think of them as assignments and it should all make sense.
This is a little surprising. So "from mod import *" really copies all of the
scalars into new variables in the local namespace. The same is true with
object pointers I suppose, but this is transparent as all the copies
access the same object.
I always ASSumed that the two forms of import were equivalent, but that
one form did away with the need to be explicit about the namespace: mod.thing
Obviously this is far from the case.
--
Posted via a free Usenet account from http://www.teranews.com
这篇关于从问题导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!