Ejemplo n.º 1
0
def getclass(obj):
    """return method cls"""
    if not check_obj(obj):
        return None
    if inspect.ismethod(obj):
        try:
            return obj.im_class  # python2
        except AttributeError:
            return obj.__self__  # python3

    if isinstance(obj, property):  # property
        return get_property_class(obj)
    if isclassmethod(obj) or isstaticmethod(obj):
        module = inspect.getmodule(obj)
        if not module:
            return
        for name in dir(module):
            attr = getattr(module, name)
            if attr is None:
                continue
            if not inspect.isclass(attr):
                continue
            cls = attr
            if hasattr(cls, obj.__name__):
                if obj == getattr(cls, obj.__name__):
                    return cls
    if hasattr(obj, "__class__") and not inspect.isroutine(obj):
        return obj.__class__
Ejemplo n.º 2
0
def _get_class(obj):
    if inspect.ismethod(obj):  # method
        return _get_instance_method_class(obj)
    if isinstance(obj, property):  # property
        return _get_property_class(obj)
    if isclassmethod(obj) or isstaticmethod(obj):
        return _get_obj_class(obj)
    return _get__class__(obj)
Ejemplo n.º 3
0
def gettype(obj):
    if inspect.ismodule(obj):
        return "module"
    if inspect.isclass(obj):
        return "class"
    if inspect.ismethod(obj):
        return"method"
    if inspect.isfunction(obj):
        return "function"
    if islambda(obj):
        return"lambda function"
    if isclassmethod(obj):
        return"classmethod"
    if isstaticmethod(obj):
        return"staticmethod"
    if isproperty(obj):
        return "property"
    if inspect.isbuiltin(obj) and inspect.isroutine(obj):
        return"builtin function"