Como escrever funções em Python
Shayne Miel
Software Architect @ Duo Security
Um gerenciador de contexto:





Gerenciadores de contexto:
Buffet:
with open('my_file.txt') as my_file:
text = my_file.read()
length = len(text)
print('The file is {} characters long'.format(length))
open() faz três coisas:
with
with <context-manager>()
with <context-manager>(<args>)
with <context-manager>(<args>):
with <context-manager>(<args>):
# Rode seu código aqui
# Este código roda "dentro do contexto"
with <context-manager>(<args>):
# Rode seu código aqui
# Este código roda "dentro do contexto"
# Este código roda depois que o contexto é removido
with <context-manager>(<args>) as <variable-name>:
# Rode seu código aqui
# Este código roda "dentro do contexto"
# Este código roda depois que o contexto é removido
with open('my_file.txt') as my_file:
text = my_file.read()
length = len(text)
print('The file is {} characters long'.format(length))
Como escrever funções em Python