3 ms·
It's ugly import math def sqr(x): return x*x class VecMag: def __init__(self, comp): self.comp = comp def __enter__(sel
by b0sk 11y ago
It's ugly
import math
def sqr(x):
return x*x
class VecMag:
def __init__(self, comp):
self.comp = comp
def __enter__(self):
self.comp = map(sqr, self.comp)
return self
def __exit__(self, type, value, traceback):
print math.sqrt(self.mag)
def sum(self):
self.mag = sum(self.comp)
comp = (1,2,3,4)
with VecMag(comp) as squared:
squared.sum()
$python test.py
5.47722557505