Funktionen in Python schreiben
Shayne Miel
Software Architect @ Duo Security
Ein Kontextmanager:





Kontextmanager:
Caterer:
with open('my_file.txt') as my_file:
text = my_file.read()
length = len(text)
print('The file is {} characters long'.format(length))
open() macht drei Dinge:
with
with <context-manager>()
with <context-manager>(<args>)
with <context-manager>(<args>):
with <context-manager>(<args>):
# Run your code here
# This code is running "inside the context"
with <context-manager>(<args>):
# Run your code here
# This code is running "inside the context"
# This code runs after the context is removed
with <context-manager>(<args>) as <variable-name>:
# Run your code here
# This code is running "inside the context"
# This code runs after the context is removed
with open('my_file.txt') as my_file:
text = my_file.read()
length = len(text)
print('The file is {} characters long'.format(length))
Funktionen in Python schreiben