Behavior when returning in the with block

Confirmation that is not interesting.

I was worried whether the context manager's termination process would be called properly when I returned in the block of the with statement.

In conclusion, it's okay.

withreturn.py


class Some(object):
    def add(self, x, y): 
        return x + y 

    def close(self):
        print "Closed"


from contextlib import closing
def foo(x):
    with closing(Some()) as some:
        return some.add(x, 10) 


print foo(16)
# Closed
#=> 26

The termination process is called in this way.

Recommended Posts

Behavior when returning in the with block
When the variable name conflicts with the Devaga command in pdb
Behavior when giving a list with shell = True in subprocess
Behavior in each language when coroutines are reused with for
Check the behavior when assigning Python
Behavior when listing in Python heapq
When the node disappears in rqt_graph
Check the behavior of destructor in Python
Display Python 3 in the browser with MAMP
When the target is Ubuntu 16.04 in Ansible
When changing the table name with flask_migrate
When the previous graph remains in Seaborn
Log in to the remote server with SSH
[Python] Get the files in a folder with Python
Behavior when SIGEV_THREAD is set in sigev_notify of sigevent with timer_create (C language)
See the behavior of drunkenness with reinforcement learning
[Automation] Extract the table in PDF with Python
Execution order when multiple context managers are specified in the Python with statement
Precautions when dealing with control structures in Python 2.6
What I did when I got stuck in the time limit with lambda python
Determine the numbers in the image taken with the webcam
Detect folders with the same image in ImageHash
Precautions when installing packages in the conda environment
How to display in the entire window when setting the background image with tkinter
Differences in the behavior of each LL language when the list index is skipped
Character encoding when dealing with files in Python 3
Behavior when Trainable = False of Container in Keras
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
Load the network modeled with Rhinoceros in Python ②
When the axis and label overlap in matplotlib
About the behavior of Model.get_or_create () of peewee in Python
Visualize the behavior of the sorting algorithm with matplotlib
Behavior when saving python datetime object in MongoDB
Load the network modeled with Rhinoceros in Python ①
The story that fits in with pip installation
It became TLE when I confirmed the operation with the print function in the competition pro
A memo when checking whether the specified key exists in the defined dictionary with python
What I did when I was angry to put it in with the enable-shared option
How to change the behavior when loading / dumping yaml with PyYAML and its details
Solved the problem that the image was not displayed in ROMol when loaded with PandasTools.LoadSDF.
When a local variable with the same name as a global variable is defined in the function
When the selected object in bpy.context.selected_objects is not returned
Change the time zone with Docker in Oracle Database
Complement the library you put in anaconda with jedi-vim
Initial settings when using the foursquare API in python
[Python] Get the numbers in the graph image with OCR
Implement Sign In With Google on the backend side
Precautions when dealing with ROS MultiArray types in Python
Crawl the URL contained in the twitter tweet with python
Mailbox selection when retrieving Gmail with imaplib in python
Convert the image in .zip to PDF with Python
Get the result in dict format with Python psycopg2
How to access with cache when reading_json in pandas
Read the linked list in csv format with graph-tool
Write letters in the card illustration with OpenCV python
Embedding in datetime when only the time is known
Load the module with the same name in another location
Check when the version does not switch with pyenv
Japanese output when dealing with python in visual studio
Participated in the first ISUCON with the team "Lunch" # ISUCON10 Qualifying
The value of meta when specifying a function with no return value in Dask dataframe apply