Skip to content

Instantly share code, notes, and snippets.

@dutc
Created July 20, 2014 04:16
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 dutc/88df5ddcc223a4bc983f to your computer and use it in GitHub Desktop.
Save dutc/88df5ddcc223a4bc983f to your computer and use it in GitHub Desktop.
custom type system using __instancecheck__
class FoobarMeta(type):
def __instancecheck__(self, obj):
return hasattr(obj, 'foo') and hasattr(obj, 'bar')
class Foobar(metaclass=FoobarMeta):
pass
class A:
def foo(self):
pass
class B:
def foo(self):
pass
def bar(self):
pass
class C:
def bar(self):
pass
if __name__ == '__main__':
a, b, c = A(), B(), C()
assert not isinstance(a, Foobar)
assert isinstance(b, Foobar)
assert isinstance(c, Foobar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment