Created
April 22, 2024 08:13
-
-
Save yancya/872500c98dbea057a1cb0826c0f1d4cb to your computer and use it in GitHub Desktop.
Python触ってて出てきたwithがなんなのか分からなかったので調べた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
@contextmanager | |
def hoge(): | |
try: | |
print("start") | |
yield "Hello, " | |
finally: | |
print("end") | |
with hoge() as msg: | |
print(msg + "World!") #=> Hello, World! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hoge | |
puts("start") | |
yield "Hello, " | |
ensure | |
puts("end") | |
end | |
hoge do |msg| | |
puts(msg + "World!") #=> Hello, World! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment