isimmutable function in Python

import string
import random


def usage():
    # int
    print(isimmutable(1))  # True

    # str
    print(isimmutable('Hello, world!'))  # True

    # user defined class
    class C:
        pass
    print(isimmutable(C()))  # False

    # dict
    print(isimmutable({'a': 1}))  # False

    # set
    print(isimmutable({'a'}))  # False

    # list
    print(isimmutable([]))  # False

    # tuple
    print(isimmutable(()))  # True


def isimmutable(obj):
    """Check the obj is immutable or not by adding new attribute or sequence."""

    #
    # attribute
    #   obj[attr]
    #

    #
    while True:
        attr_name = random_str()
        if not hasattr(obj, attr_name):
            break
    try:
        setattr(obj, attr_name, None)
    except (AttributeError, TypeError):
        pass
    else:
        delattr(obj, attr_name)
        return False

    #
    # sequence
    #   obj[key]
    #
    if not hasattr(obj, '__iter__'):
        return True

    # dict
    while True:
        key_name = random_str()
        if key_name not in obj:
            break
    try:
        obj[key_name] = None
    except (AttributeError, TypeError):
        pass
    else:
        del obj[key_name]
        return False

    # set
    while True:
        element = random_str()
        if element not in obj:
            break
    try:
        # to avoid overriding add method.
        # x obj.set(element)
        set.add(obj, element)
    except (AttributeError, TypeError):
        pass
    else:
        set.remove(obj, element)
        return False

    # list
    try:
        # to avoid overriding append method.
        # x obj.append(element)
        list.append(obj, None)
    except (AttributeError, TypeError):
        pass
    else:
        list.pop(obj)
        return False

    return True


def random_str():
    N = 5
    return ''.join([random.choice(string.ascii_letters) for i in range(N)])


if __name__ == '__main__':
    usage()