Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.