fibo.py
可以说我有一个定义如下的python模型:
#Fibonacci numbers module
print "This is a statement"
def fib(n):
a,b = 0,1
while b < n:
print b
a, b = b, a+b
def fib2(n):
a,b = 0,1
result= []
while(b < n):
result.append(b)
a, b = b, a+b
return result
在我的口译会话中,我执行以下操作:
>> import fibo
This is a statement
>>> fibo.fib(10)
1
1
2
3
5
8
>>> fibo.fib2(10)
[1, 1, 2, 3, 5, 8]
>>> fibo.__name__
'fibo'
>>>
到目前为止一切顺利......重新启动解释器:
>>> from fibo import fib,fib2
This is a statement
>>> fibo.__name__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fibo' is not defined
>>>
我预计会出现错误,因为我只导入了fib
和fib2
. 但是我不明白为什么当我只导入fib
和时打印声明fib2
。
其次,如果我将模块更改为:
#Fibonacci numbers module
print "This is a statement"
print __name__
预期的结果应该是什么?