with open("example.json") as file_handle: data = file_handle.read() file_handle = open("example.json") data = file_handle.read() file_handle.close() file_handle = open("example.json") try: data = file_handle.read() except IOError: pass except Exception: print("Got exception") else: print("Got no excpetion") finally: file_handle.close() class ContextManager(object): def __enter__(self): pass def __exit__(self, type, value, traceback): pass context_guard = ContextManager() value = context_guard.__enter__() exc = True try: try: var = value pass # Here the code in the context # would go. except: exc = False if not context_guard.__exit__(*sys.exc_info()): raise finally: if exc: context_guard.__exit__(None, None, None) import sys from StringIO import StringIO class redirect_stdout: def __init__(self, target): self.stdout = sys.stdout self.target = target def __enter__(self): sys.stdout = self.target def __exit__(self, type, value, tb): sys.stdout = self.stdout out = StringIO() with redirect_stdout(out): print("Prints to sys.stdout") print("This too") out.getvalue() def my_add(x, y): return x + y def my_subtract(x, y): return x - y def my_multiply(x, y): return x * y def my_add(x, y): print(x, y) return x + y my_add(6,7) def print_args(function): def wrapper(*args, **kwargs): print("args: {}".format(args)) print("kwargs: {}".format(kwargs)) return function(*args, **kwargs) return wrapper def my_add(x, y): return x + y my_add = print_args(my_add) my_add(3,4) @print_args def my_subtract(x, y): return x - y my_subtract(5,2) my_add(1,2,3,5, a1=1, a2=4) class CLS(object): def __init__(self): self.a = 1 obj = CLS() obj.a class CLS(object): def __init__(self): self.b = 3. self.c = 2. @property def a(self): return self.b / self.c obj = CLS() obj.a obj.b = 4. obj.a class CLS(object): def __init__(self): self.b = 3. self.c = 2. @property def a(self): return self.b / self.c @a.setter def a(self, value): self.c = 1.0 self.b = value obj = CLS() print("Initial b, c, a: \n{}, {}, {}".format(obj.b, obj.c, obj.a)) obj.a = 5.0 print("") print("b, c, a after a being set: \n{}, {}, {}".format(obj.b, obj.c, obj.a)) class A(object): a = [] class B(A): def __init__(self, name): self.name = name self.a.append(self.name) def print_list(self): print(self.a) b1 = B("1") b2 = B("2") b1.print_list() b2.a is b1.a b3 = B("3") b1.print_list() A.a del b3 b1.print_list() b3 class A(object): a = [] class B(A, object): def __init__(self, name): self.name = name self.a.append(self.name) def print_list(self): print(self.a) def __del__(self): self.a.remove(self.name) b1 = B("1") b2 = B("2") b3 = B("3") b1.print_list() del b3 b1.print_list() class A(object): a = [] class B(A): def __init__(self, name): self.name = name self.a.append(self.name) def print_list(self): print(self.a) def __del__(self): self.a.remove(self.name) def __add__(self, other): combined_name = "".join(sorted([self.name, other.name])) combined_b = B(combined_name) return combined_b b1 = B("1") b2 = B("2") b12 = b2 + b1 b1.print_list() import numpy as np a = np.array([1., 2., 3.]) a a * 4.0 type(a) b = np.array([[1, 4, 6], [5, 2, 2]]) b b[1][0] b[1, 0] b[:, 1:2] b[1:2, 1:2] = 0 b b.shape