Skip to content

Instantly share code, notes, and snippets.

@ionelmc
Created January 13, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionelmc/553372b9322a3edcccfe to your computer and use it in GitHub Desktop.
Save ionelmc/553372b9322a3edcccfe to your computer and use it in GitHub Desktop.
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