You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog_files/pysheet.md
+44Lines changed: 44 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3689,6 +3689,50 @@ None
3689
3689
3. Keyword argumentswith default values make it easy to add new behaviors to a function, especially when the function has existing callers.
3690
3690
4. Optional keyword arguments should always be passed by keyword instead of by position.
3691
3691
3692
+
## Context Manager
3693
+
3694
+
While Python's context managers are widely used, few understand the purpose behind their use. These statements, commonly used with reading and writing files, assist the application in conserving system memory and improve resource management by ensuring specific resources are only in use for certain processes.
3695
+
3696
+
### with statement
3697
+
3698
+
A context manageris anobject thatis notified when a context (a block of code) startsand ends. You commonly use onewith thewith statement. It takes care of the notifying.
3699
+
3700
+
For example,file objects are context managers. When a context ends, thefileobjectis closed automatically:
3701
+
3702
+
```python
3703
+
>>>withopen(filename)as f:
3704
+
>>>file_contents= f.read()
3705
+
3706
+
# the open_file object has automatically been closed.
3707
+
```
3708
+
3709
+
Anything that ends execution of the block causes the context manager's exit method to be called. This includes exceptions, and can be useful when an error causes you to prematurely exit from an open file or connection. Exiting a script without properly closing files/connections is a bad idea, that may cause data loss or other problems. By using a context manager you can ensure that precautions are always taken to prevent damage or loss in this way.
3710
+
3711
+
### Writing your own contextmanager using generator syntax
3712
+
3713
+
Itis also possible to write a context manager using generator syntax thanks to the```contextlib.contextmanager``` decorator:
3714
+
3715
+
```python
3716
+
>>>import contextlib
3717
+
>>>@contextlib.contextmanager
3718
+
...def context_manager(num):
3719
+
...print('Enter')
3720
+
...yield num+1
3721
+
...print('Exit')
3722
+
>>>with context_manager(2)as cm:
3723
+
...# the following instructions are run when the 'yield' point of the context
3724
+
...# manager is reached.
3725
+
...# 'cm' will have the value that was yielded
3726
+
...print('Right in the middle with cm ={}'.format(cm))
3727
+
3728
+
Enter
3729
+
Rightin the middlewithcm=3
3730
+
Exit
3731
+
3732
+
>>>
3733
+
3734
+
```
3735
+
3692
3736
## Virtual Environment
3693
3737
3694
3738
The use of a Virtual Environmentis to test python codein encapsulated environmentsand to also avoid filling the base Python installationwith libraries we might usefor only one project.