Módulos#
Última modificación: Mayo 14, 2022
Introducción#
[1]:
%%writefile fibo.py
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
Overwriting fibo.py
[2]:
import fibo
fibo.fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
[3]:
fibo.fib2(100)
[3]:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
[4]:
fib = fibo.fib
fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Importación#
[5]:
from fibo import fib, fib2
fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
[6]:
from fibo import *
fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
[7]:
import fibo as fib
fib.fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Ejecución de módulos como scripts#
[8]:
%%writefile fibo.py
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
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Overwriting fibo.py
[9]:
!python3 fibo.py 50
0 1 1 2 3 5 8 13 21 34
Módulos estándar#
[10]:
import sys
sys.ps1
[10]:
'In : '
[11]:
sys.ps2
[11]:
'...: '
Función dir()#
[12]:
import fibo
dir(fibo)
[12]:
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'fib',
'fib2']
Paquetes#
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/ Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...
import sound.effects.echo
sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
from sound.effects import echo
echo.echofilter(input, output, delay=0.7, atten=4)
from sound.effects.echo import echofilter
echofilter(input, output, delay=0.7, atten=4)
from sound.effects import *
Referencias entre paquetues#
from . import echo
from .. import formats
from ..filters import equalizer