Skip to content

Instantly share code, notes, and snippets.

@danielballan
Created January 20, 2014 23:09
Show Gist options
  • Save danielballan/8531266 to your computer and use it in GitHub Desktop.
Save danielballan/8531266 to your computer and use it in GitHub Desktop.
forgivness vs. permission
In [4]: def forgiveness(x):
try:
x.plugins.append(1)
except AttributeError:
x.plugins = [1]
...:
In [5]: def permission(x):
if not hasattr(x, 'plugins'):
x.plugins = []
x.plugins.append(1)
...:
In [6]: class X(object):
...: pass
...:
In [7]: x = X()
In [8]: %timeit forgiveness(x)
1000000 loops, best of 3: 233 ns per loop
In [9]: %timeit permission(x)
1000000 loops, best of 3: 333 ns per loop
In [10]:
In [10]: x.plugins = []
In [11]: %timeit forgiveness(x)
1000000 loops, best of 3: 232 ns per loop
In [12]: %timeit permission(x)
1000000 loops, best of 3: 331 ns per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment