Advertisement
DeaD_EyE

contextlib.closing

Jul 1st, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. from contextlib import closing
  2. import serial
  3.  
  4. ##############################################
  5.               Better example
  6. ##############################################
  7.  
  8. # https://docs.python.org/2.7/library/contextlib.html
  9. # https://docs.python.org/3.5/library/contextlib.html
  10.  
  11. with closing(serial.Serial('/dev/ttyUSB0', baudrate=11520)) as ser:
  12.     print(ser.inWaiting())
  13.     # the function closing calls the method
  14.     # close of the wrapped object when leaving
  15.     # the context
  16.  
  17. # Here is the serial connection already closed :-)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement