Sponsored Content
Full Discussion: Context Managers in Python
Top Forums Shell Programming and Scripting Context Managers in Python Post 302954900 by Aia on Saturday 12th of September 2015 01:00:19 PM
Old 09-12-2015
A Context Manager is any class that must, at least, implement a __enter__() method and a __exit__() method. The purpose of this class is to setup, or build up an object and tear it down when you are done with it.
The __enter__() method would initialize the object, it would utilized the try, except and finally statements, and it would produce a proper return.
The __exit__() method would do the proper clean up after the object is not necessary any longer.

A common example of using a Context Manager is with opening files.
If you do it manually, you would have to do the checks necessary: that the file exists, that it can be accessed and opened, etc. After that you need to release the object file by closing it. This setup and tear-down action can be taken care of, nicely, using a Context Manager.

Last edited by Aia; 09-12-2015 at 08:57 PM..
 

4 More Discussions You Might Find Interesting

1. Linux

Question regarding window managers

Does anyone know of an article, or tutorial concerning the development of a window manager ? I would like to create my own window manager, and maybe even a small lightweight widget set, but I haven't been able to find any texts containing the theory behind the creation of a new window manager.... (4 Replies)
Discussion started by: NanoSec
4 Replies

2. Solaris

Solaris 10 Window Managers

Hello all, I just discovered something weird in my system, well maybe you already knew it, but it's new for me, i was looking for a file when i ended up in the /opt/sfw folder, and well, first i saw the bin folder, so i entered and discovered a bunch of programs that installed with the companion... (0 Replies)
Discussion started by: sx3v1l_1n51de
0 Replies

3. Shell Programming and Scripting

Questions related to if in awk context and if without awk context

I wrote this code, questions follow #! /bin/bash -f # Purpose - to show how if syntax is used within an awk clear; ls -l; echo "This will print out the first two columns of the inputted file in this directory"; echo "Enter filename found in this directory"; read input; ... (11 Replies)
Discussion started by: Seth
11 Replies

4. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies
lp-shell(1)						      General Commands Manual						       lp-shell(1)

NAME
lp-shell - Open an interactive launchpadlib shell. SYNOPSIS
lp-shell [-a] [-cPROGRAM] [--python] [--ipython] [service] [LP API version] DESCRIPTION
lp-shell opens an interactive Python shell with a launchpadlib.Launchpad object "lp" which is ready for use. It authenticates against Launchpad with the consumer name "udt-lp-shell". When using lp-shell with the -a option it will use the anonymous login from launchpadlib.Launchpad. By default lp-shell connects to the "production" Launchpad service using the "1.0" LP API version. If you want to connect to another Launchpad service, call lp-shell with the service name as the second argument. lp-shell supports all ser- vices known by launchpadlib Python module. Currently known are (list can be incomplete or outdated): "production", "staging", "dogfood". A different LP API version can be selected by passing the API version to use as the third argument. Current supported are: "beta", "1.0" and "devel". OPTIONS
-a Login anonymously into Launchpad. -c PROGRAM Don't enter a shell but only run the specified Python program and exit. --ipython Use an ipython shell if available (default). --python Use a regular python shell. AUTHORS
lp-shell was written by Martin Pitt <martin.pitt@ubuntu.com>. It is released under the terms of the GNU General Public License, version 2 or (at your option) any later version. lptools 27 March 2010 lp-shell(1)
All times are GMT -4. The time now is 10:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy