In the previous episodes
- basic stuff, syntax
- functions (wrote our cool min function) + functional stuff
- scopes, PEP-8
- strings, bytes
- collections
- classes basics
- decorators, functools
- exceptions, context managers
- iterators
👻 Generators
👇 Generator
after yield it stops and comes back later
unique
map
chain
count
How to reuse: don’t.
or use tee
from ìtertools
yield
send!
throw
and close()
yield from and return
generators and context managers
useful example?
Generators wrap-up
- generator is a function which use yield and yield from
- generators are everywhere
- generators can be used as iterators, coroutines, easy context managers and others!
islice
count, cycle, repeat
chain
chain.from_iterable(it)
vs chain(*it)
?
tee
don’t use it
after copying
combinatorics!
- useful
- combinatorics,
- islice,
- count, cycle, repeat
- tee
➡️ Modules, imports and other stuff
http://www.dabeaz.com/modulepackage/
1️⃣ Modules
module.py
with tempfile
python ./module.py
import
sys.path
Modules wrap-up
- module is a
*.py
file from which you cam import from
- import from, import as
- byte code is executed from up to down
- sort imports
2️⃣ Packages
package package
absolute / relative import
executable package
Now we can run it!
Packages wrap-up
- package is a way to group Python code
- every directory with
__init__.py
makes a package
- use absolute imports
- adding
__main__.py
allows you to execute your package
🏁 Any questions?