Created
January 13, 2016 15:11
-
-
Save ionelmc/553372b9322a3edcccfe to your computer and use it in GitHub Desktop.
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
import pytest | |
class A(object): | |
def __init__(self, **kw): | |
for k, v in kw.items(): | |
setattr(self, k, v) | |
class B(object): | |
a = 1 | |
class C(object): | |
pass | |
with_obj = pytest.mark.parametrize("obj", [object(), C, A(a=1), B], | |
ids=['without-obj', 'without-cls', 'withattr-obj', 'withattr-cls']) | |
@with_obj | |
def test_hasattr(benchmark, obj): | |
@benchmark | |
def func(): | |
return hasattr(obj, 'a') | |
@with_obj | |
def test_getattr(benchmark, obj): | |
@benchmark | |
def func(sentinel=object()): | |
return getattr(obj, 'a', sentinel) is not sentinel | |
@with_obj | |
def test_tryexcept(benchmark, obj): | |
@benchmark | |
def func(): | |
try: | |
obj.a | |
except: | |
return False | |
else: | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment