phép nói rằng tôi có một mô hình python fibo.py được xác định như sau:Python: Nhập khẩu Mô-đun
#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
Trong phiên dịch viên của tôi, tôi làm như sau:
>> 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'
>>>
Cho đến nay rất tốt. .restart người phiên dịch:
>>> 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
>>>
tôi mong đợi các lỗi như tôi đã chỉ nhập khẩu fib và fib2. Nhưng tôi không hiểu tại sao câu lệnh được in ra khi tôi chỉ nhập khẩu fib và fib2.
Thứ hai nếu tôi thay đổi các module như:
#Fibonacci numbers module
print "This is a statement"
print __name__
gì phải là kết quả mong đợi?
bạn có thể sử dụng một hàm duy nhất với 'yield b': 1.' cho f trong fib (n): print f' 2. 'result = list (fib (n))' – jfs