Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

PyBites: Code Challenge 09 - Give the With Statement some Love and Create a Cont ...

$
0
0

This week, each one of you has a homework assignment ... - Tyler Durden (Fight club)

A new week, time for some coding!

This week we have a free form exercise. After reading Dan's great article on the with statement we thought it would be cool to ask our PyBites community to come up with creative uses of the with statement. This week you get to implement your own Context Manager.

You can either:

Define a class implementing the __enter__ and __exit__ methods. Dan shows an Indenter class as example in his article.

Use the nice @contextmanager decorator shortcut.

Have fun!

Other resources

with statement

PEP 343 -- The "with" Statement

contextlib ― Utilities for with-statement contexts

python with Context Managers

If beginner you might need to read up on some more advanced concepts: Generators and Decorators , also covered in chapters 3 and 7 of the Python tips book .

To follow along with our challenges

Start coding by forking our challenges repo :

$ git clone https://github.com/pybites/challenges

If you already forked it sync it :

# assuming using ssh key $ git remote add upstream git@github.com:pybites/challenges.git $ git fetch upstream # if not on master: $ git checkout master $ git merge upstream/master $ cd 09 # open withlove.py (just a blank file for this challenge) # code

Remember: there is no best solution, only learning more/ better Python. We're looking forward reviewing our and your solutions end of this week. Good luck and have fun!

About PyBites Code Challenges

More background in our first challenge article .


Viewing all articles
Browse latest Browse all 9596

Trending Articles