Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf3414b3

Browse files
authored
Merge pull requestwilfredinni#29 from Sonatrix/master
Add context manager introduction
2 parents4b8514e +65fa0bc commitf3414b3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎blog_files/pysheet.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,6 +3689,50 @@ None
36893689
3. Keyword argumentswith default values make it easy to add new behaviors to a function, especially when the function has existing callers.
36903690
4. Optional keyword arguments should always be passed by keyword instead of by position.
36913691

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+
36923736
## Virtual Environment
36933737

36943738
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.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp