#Python has had the concept of context managers for a loooong time!

Let's talk about context managers again!

๐Ÿงต๐Ÿ๐Ÿ‘‡
The `with` statement, which is the normal way for working with context managers, was added back in Python 2.5!

Here is a pretty common example of using a context manager: Image
The beauty of a context manager is that they allow you to do some setup and teardown automatically.

The downside is that is abstracted away and can sometimes make the code less obvious when debugging
While there are many things in Python that are built-in context managers, you can also write your own!

All you need to do is create a class and override the following methods:

๐Ÿ __enter__
๐Ÿ __exit__ Image
When you use the `with` statement, it will take the return value from the `__enter__` method. You can assign that to a value or throw it away.

In the following example, you turn `self.conn` into `data`

with DataConn() as data:
pass Image
When you drop out of the `with` statement's code block, the `__exit__()` method is called.

Here is where you put your cleanup code. You can catch errors here too and handle them as you see fit.

Some devs will log the error and then re-raise it, for example Image
I also wrote up a thread on using #Python's awesome `contextlib` module which makes creating context managers even easier:

I hope you enjoyed this quick review of #Python context managers.

Follow me for more great content on Python!

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

19 Nov
Today we are going to talk about the basics of unit testing in #Python

๐Ÿงต๐Ÿ๐Ÿ‘‡
The Python programming language includes built-in packages for testing:

๐Ÿ doctest
๐Ÿ unittest
I already did a thread on Python's `doctest`:

Read 13 tweets
18 Nov
Python comes with its own built-in debugger called `pdb`.

Let's talk about that in a mini-thread!

๐Ÿงต๐Ÿ๐Ÿ‘‡
`pdb` stands for Python debugger. It is a built-in part of Python's standard library

Here is a link to the documentation:

docs.python.org/3/library/pdb.โ€ฆ
To start debugging code with `pdb`, you need to write some code.

Here's the code you'll be using: Image
Read 17 tweets
17 Nov
One big gotcha when it comes to #Python decorators is that the decorator will covertly replace the function's name and docstring with its own (๐Ÿงต๐Ÿ๐Ÿ‘‡)

Check out the following example Image
When you run the decorated code in the previous tweet, you will see that the decorated function's name and docstring have disappeared! Image
You may be wondering, why do I care if a function's name and docstring are replaced?

The reason this is important is that your introspection won't work correctly anymore. If you run Python's `help()` built-in against your decorated function, it will give you the wrong info
Read 7 tweets
15 Nov
Logging in #Python is a popular topic and it's a really useful one to understand.

Today we are going to learn more about that!

๐Ÿงต๐Ÿ๐Ÿ‘‡
Python includes the `logging` module as a part of its standard library.

You can get started using it quite easily:
Python's `logging` module supports multiple logging levels:

๐Ÿ notset
๐Ÿ debug
๐Ÿ info
๐Ÿ warning
๐Ÿ error
๐Ÿ critical
Read 17 tweets
12 Nov
One of the harder concepts to learn in #Python are decorators.

So let's take a few moments and learn about them!

๐Ÿงต๐Ÿ๐Ÿ‘‡
It's always good to start with a regular function. Why?

Because you create a decorator using functions!

This function, `doubler()` takes in a number and doubles it. That's it! Image
But wait! #Python functions are also objects. You can get their `__name__` and their docstring (`__doc__`), if they have one.

You can also get a listing of their other methods with `dir()`

What all this means is that you can pass a function to another function! Image
Read 11 tweets
8 Nov
Did you know you can watermark images with #Python?

Find out how using the #Pillow package in this mini-thread!

๐Ÿงต๐Ÿ๐Ÿ‘‡
If you want to add text, such as a URL, to an image, you can do that pretty easily with #Python and #pillow

The following code snippet is only 21 lines!
Here are the before and after photos of the Yequina lighthouse showing how to add a watermark with #Python
Read 5 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(