# Python模块

```# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()

def fib2(n):   # return Fibonacci series up to n
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a+b
return result```

`>>> import fibo`

```>>> fibo.fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'```

```>>> fib = fibo.fib
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377```

## 有关模块的更多信息

`import` 语句有一个变体，它可以把名字从一个被调模块内直接导入到现模块的符号表里。例如:

```>>> from fibo import fib, fib2
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377```

```>>> from fibo import *
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377```

```>>> import fibo as fib
>>> fib.fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377```

It can also be used when utilising `from` with similar effects:

```>>> from fibo import fib as fibonacci
>>> fibonacci(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377```

### 以脚本的方式执行模块

`python fibo.py `

```if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))```

```\$ python fibo.py 50
0 1 1 2 3 5 8 13 21 34```

```>>> import fibo
>>>```