Created
January 20, 2014 23:09
-
-
Save danielballan/8531266 to your computer and use it in GitHub Desktop.
forgivness vs. permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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