---
title: "Goose typing"
slug: goose-typing
url: https://listedarticles.com/articles/goose-typing
canonical_url: https://www.pythonmorsels.com/goose-typing/
content_type: tutorial
language: en
published_at: 2026-08-07T19:00:00.000Z
updated_at: 2026-11-18T01:50:14.445Z
author: "Trey Hunner"
authored_by: human
publisher: "Python Morsels"
topics: ["python", "programming"]
license: all-rights-reserved
word_count: 1604
reading_minutes: 7
citation: "Trey Hunner, Python Morsels. \"Goose typing.\" 7 Aug 2026. https://www.pythonmorsels.com/goose-typing/ (all-rights-reserved)"
# The full text follows. The web page shows an extract and sends readers
# to the source above; quote the citation and link the canonical URL.
---

# Goose typing

> Trey Hunner explains how Python's isinstance and issubclass can coexist with duck typing: abstract base classes and __subclasshook__ let you check behavior rather than concrete type, a practice known as goose typing.

You may have heard that in Python type checking is frowned upon and "duck typing" is embraced instead.
Python's `isinstance` function might seem like a tool that runs against our tendency for duck typing.
After all, the `isinstance` function checks types and if we're type checking then we aren't duck typing, right?

Incorrect!
Interestingly, we can actually use `isinstance` *and* practice duck typing at the same time!
Python's `isinstance` function can actually be a trusty tool in the land of duck typing.

First, let's review what [duck typing](https://www.pythonmorsels.com/duck-typing/) means in Python.

## Duck typing

In Python we tend to care about the *behavior* of objects much more than the *type* of objects.

For example the built-in `sum` function accepts any iterable of numbers.

```
>>> numbers = [2, 1, 3, 4, 7]
>>> sum(numbers)
17
```

It doesn't care what *type* the iterable is.
It simply works with *any* iterable.

Likewise, Python's `print` function accepts [a `file` argument](https://www.pythonmorsels.com/printing-file/) which can be any object that has a `write` method:

```
>>> class FakeFile:
...     def __init__(self):
...         self.text = ""
...     def write(self, text):
...         self.text += text
...
>>> f = FakeFile()
>>> print("Hello world", file=f)
>>> f.text
'Hello world\n'
```

The `file` argument's value *should* be an object that acts like a file, but `print` doesn't check that.
It just calls its `write` method on the given object and hopes for the best.

This focus on behavior instead of type is called [duck typing](https://www.pythonmorsels.com/duck-typing/).
As the saying goes:

> If it looks like a duck and quacks like a duck, it's a duck.

In Python, instead of checking an animal's DNA to know what it is, we just observe how it acts.

## Python's `isinstance` function

Python's `isinstance` function checks whether an object is of a certain type or whether it's a subclass of that type:

```
>>> numbers = [2, 1, 3, 4, 7]
>>> isinstance(numbers, list)
True
>>> isinstance(numbers, object)
True
```

The `isinstance` function might seem like a tool that runs against our tendency for duck typing.

If we're type checking then we aren't duck typing, right?

Yes and no...

## `isinstance` doesn't just check types

It's *actually possible* to use `isinstance` and practice duck typing at the same time!

We have three classes here, two of which are iterables (because they [implement a `__iter__` method](https://www.pythonmorsels.com/how-to-make-an-iterable/)):

```
from collections.abc import Iterable

class A(Iterable):
    """Iterable inheriting from Iterable."""
    def __iter__(self):
        yield 1
        yield 2

class B:
    """Not an iterable."""

class C:
    """Iterable that doesn't inherit from Iterable."""
    def __iter__(self):
        yield 3
        yield 4
```

One of these classes inherits from the `Iterable` class in Python's `collections.abc` module.
If we check whether its objects are instances of that class we'll see that they are:

```
>>> from collections.abc import Iterable
>>> a = A()
>>> isinstance(a, Iterable)
True
```

The next class doesn't inherit from `Iterable` and it also *isn't* iterable.
If we check whether its objects are instances of `Iterable`, we'll see that they aren't:

```
>>> b = B()
>>> isinstance(b, Iterable)
False
```

This last class *is* an iterable, but it doesn't inherit from the `Iterable` class.
If we ask Python whether its objects are instances of `Iterable`, what do you think we'll see?

```
>>> c = C()
>>> isinstance(c, Iterable)
```

In strict type checking terms, it seems like the answer should be `False`.
After all, the `C` class doesn't inherit from `Iterable`, so how could an instance of this class be considered an instance of the `Iterable` class?

Interestingly... the answer is `True`!

```
>>> isinstance(c, Iterable)
True
```

What's going on here?

## Customizing `isinstance` and `issubclass` checks

Python's `isinstance` function is for checking whether an object is an instance of a particular class.
But the behavior of Python's `isinstance` function can be customized!

**Dragons ahead!** We're about to dive into two [dunder methods](https://www.pythonmorsels.com/what-are-dunder-methods/) that necessitate a cursory knowledge of [metaclasses](https://www.pythonmorsels.com/what-are-metaclasses/), which are a fairly advanced Python topic.

Here's a `GooseMeta` metaclass which includes a `__instancecheck__` method:

```
class GooseMeta(type):
    def __instancecheck__(self, instance):
        return hasattr(instance, "honk")
```

And here's a `Goose` class which uses this metaclass:

```
class Goose(metaclass=GooseMeta):
    """Customizes isinstance to check for a honk method."""
```

**Metaclasses customize the behavior of classes** in *the same way* that classes customize the behavior of their instances.

If a class's metaclass defines an `__instancecheck__` method, that method will be used whenever the built-in `isinstance` function is used!
For example this `Car` class implements a `honk` method:

```
class Car:
    def honk(self):
        print("Honk!")
```

So if we use `isinstance` to ask whether a `Car` object is also a `Goose`, we'll see that it is:

```
>>> my_car = Car()
>>> isinstance(my_car, Goose)
True
```

This happens because whenever we use the built-in `isinstance` function, Python calls the `__instancecheck__` method on our `Goose` class with the instance:

```
>>> Goose.__instancecheck__(my_car)
True
```

Remember that since [metaclasses](https://www.pythonmorsels.com/what-are-metaclasses/) are a class's class, that `__instancecheck__` method exists on our class because its class (the metaclass) defines it.

Note that while `isinstance` works with `Goose`, if we use the `issubclass` function to ask whether the `Car` class is a subclass of `Goose`, we'll see that it isn't:

```
>>> issubclass(Car, Goose)
False
```

...unless we also implement a `__subclasscheck__`!

```
class GooseMeta(type):
    def __instancecheck__(self, instance):
        return hasattr(instance, "honk")
    def __subclasscheck__(self, subclass):
        return hasattr(subclass, "honk")

class Goose(metaclass=GooseMeta):
    """Customizes isinstance to check for a honk method."""
```

Now the `Car` class will also be seen as a subclass of `Goose`:

```
>>> issubclass(Car, Goose)
True
```

Whenever we use the built-in `issubclass` function, Python calls the `__subclasscheck__` method:

```
>>> Goose.__subclasscheck__(Car)
True
```

The `Car` class isn't technically a subclass of `Goose`, but it's seen as a sort of **virtual subclass**.
The `Goose` class' metaclass has declared that due to the behavior the `Car` class implements, cars are geese!

Alex Martelli coined the term "**goose typing**" to describe this ability in Python to practice duck typing while *also* using `isinstance` and `issubclass`.

## Goose typing with ABCs

In Python, we can use dunder methods to customize the behavior of many built-in functions on our objects: `len`, `str`, `bool`, and `int` for example.
We can *even* use dunder methods to **customize the behavior of the built-in `isinstance` and `issubclass` functions** on our objects!
Those customizations need to happen on metaclasses though, which makes them slightly trickier to implement.

To customize `isinstance` and `issubclass` checks you'll need to implement the `__instancecheck__` and `__subclasscheck__` methods on your class's metaclass.
But customizing those checks is even simpler if you're making an **abstract base class**.

[Abstract base classes](https://docs.python.org/3/glossary.html#term-abstract-base-class) inherit from Python's `ABC` class in the `abc` module:

```
import abc

class Goose(abc.ABC):
    @abc.abstractmethod
    def honk(self):
        pass
    @classmethod
    def __subclasshook__(cls, subclass):
        return (
            hasattr(subclass, "honk")
            or super().__subclasshook__(subclass)
        )

class Car:
    def honk(self):
        print("Honk!")

class Duck:
    def quack(self):
        print("Quack!")
```

Abstract base classes are their own complex topic, separate from goose typing.
But they're also quite helpful for creating classes that allow for goose typing.

Abstract base classes also support a `__subclasshook__` [class method](https://www.pythonmorsels.com/class-methods/).
By implementing `__subclasshook__` on our ABC, we can customize both instance and subclass checks.

The `__subclasshook__` method is not *actually* an official dunder method that the Python interpreter understands.
This dunder method is specific to abstract base classes, similar to the way `__post_init__` is specific to [dataclasses](https://www.pythonmorsels.com/dataclasses/).
The metaclass of `abc.ABC` (`abc.ABCMeta`) implements a `__subclasscheck__` method that calls `__subclasshook__` plus a bit more than that.

So now our `Goose` class thinks that `Car` objects are geese but `Duck` objects are not (due to the presence or absence of a `honk` method, which we check for in our `__subclasshook__` method):

```
>>> car = Car()
>>> duck = Duck()
>>> isinstance(car, Goose)
True
>>> isinstance(duck, Goose)
False
```

Although if we wanted to tell Python that our `Duck` class *should* be seen as a `Goose`, we can actually register it with the `Goose` class:

```
>>> Goose.register(Duck)
```

All ABC classes have [a `register` method](https://docs.python.org/3/library/abc.html#abc.ABCMeta) which will add our class to a registry collection that abstract base classes *also* check every time their metaclass's `__instancecheck__` and `__subclasscheck__` methods are called.

So now all `Duck` objects will be seen as `Goose` objects as well:

```
>>> isinstance(duck, Goose)
True
```

Although we probably shouldn't say that `Duck` is a `Goose` since it doesn't have a `honk` method!

You can think of abstract base classes that rely on registering as similar to **an opt-in duck**.
If **it says it's a duck**, then **it's a duck**!

## Practice goose typing with Python's pre-made ABCs

Python's `collections.abc` module includes abstract base classes that describe common Python protocols: iterables, [mappings](https://www.pythonmorsels.com/creating-a-mapping/), [sequences](https://www.pythonmorsels.com/make-a-sequence/), [hashable objects](https://www.pythonmorsels.com/making-hashable-objects/) and more.

Third-party libraries which implement Python's various protocols *tend* to either inherit from the corresponding class in the `collections.abc` module or register their objects with those abstract base classes.
That means the best way to check whether an object is an iterable, a sequence, a mapping, or a hashable object is to use `isinstance` in combination with the appropriate `collections.abc` class:

```
>>> b = bytearray([1, 2, 3])
>>> isinstance(b, Sequence)
True
```

And whenever you're implementing a Python protocol yourself, it's wise to either inherit from the appropriate abstract base class:

```
from collections.abc import MutableSequence

class MySequence(MutableSequence):
    ...
```

Or, if you'd prefer not to inherit from one of those ABCs, you could register your class with it instead:

```
from collections.abc import MutableSequence

class MySequence:
    ...

MutableSequence.register(MySequence)
```

That will ensure that `isinstance` and `issubclass` checks against the appropriate base classes will work as expected with your objects!

## Use `isinstance` and `issubclass` *with* duck typing

Statically-typed languages, like Java, prefer type checking.
In Python type checking is generally frowned upon: we practice duck typing instead.

But duck typing and the `isinstance` function are *not* mutually exclusive.
Python's `ABC` classes allow classes to customize the behavior of their `isinstance` and `issubclass` checks.
That allows for the practice of **goose typing**, meaning practicing duck typing *with* the built-in `isinstance` function.
