-1

How to check properly that a variable value is an instance of any class?

For example:

class Foo:
    pass

isinstance_of_any_class(isinstance)  # Return False
isinstance_of_any_class(Foo)  # Return False
isinstance_of_any_class(Foo())  # Return True

I have looked at:

  • isinstance - takes two mandatory arguments: object and classinfo
  • Determine if a variable is an instance of any class - question's author wanted to distinguish instances of builtin types from user-defined, but I want to distinguish instances from methods, function, classes an so on
  • The inspect module - doesn't have isinstance only the following members that near needed: ismodule, isclass, ismethod, isfunction, isgeneratorfunction, isgenerator, iscoroutinefunction, iscoroutine, isawaitable, isasyncgen, istraceback, isframe, iscode, isbuiltin, isroutine, isabstract, ismethoddescriptor, isdatadescriptor, isgetsetdescriptor, ismemberdescriptor

The desired function could be implemented by negation of every other possible object type through inspect's members. How can I do it without enumerating all of inspect's members?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Nick Vee
  • 621
  • 2
  • 7
  • 17
  • Is it possible for you to do it like this? https://stackoverflow.com/a/395782/8702713 – thisisjaymehta Jun 27 '20 at 09:57
  • @thisisjaymehta Thanks for response! No, in my question is assumed that you know nothing about the possible classes of the object if it is instance – Nick Vee Jun 27 '20 at 10:09
  • If you do `type(Foo)` and `type(isinstance)` you can see they both *are* instances of a class. Everything in Python is. – jonrsharpe Jun 27 '20 at 10:17
  • @jonrsharpe , and how your comment helps me and other who wants to distinguish somehow more intuitive instances from everything that can be checked by `inspect` module members. See "Extra" section, please. – Nick Vee Jun 27 '20 at 10:23
  • 1
    What is the *context* in which you're trying to do that? What problem is it solving? – jonrsharpe Jun 27 '20 at 10:30
  • My class accepts the callable object: it can be something that accepts `object()` usage. But only for class instances (that not a function, a class definition and something like that) I want to take instance variable with known name. Nevertheless, I can figure out the workaround in current problem by myself. I am curious about the topic in general. – Nick Vee Jun 27 '20 at 10:54

1 Answers1

0

What you are asking can be a bit tricky, I could not find any helpful documentation except for the inspect module.

Anyway, This will cover most of the cases:

import inspect

def isinstance_of_any_class(o):
  if inspect.isclass(o): return False # Is a class or a type such int, float...
  elif not hasattr(o, '__dict__'): return False # Is a function
  else: return True
Guillem
  • 2,376
  • 2
  • 18
  • 35
  • Not everything without a `__dict__` is a function. You can write classes with `__slots__`, for example, and instances of built-in types often don't have it either (`hasattr(1, '__dict__')`). – jonrsharpe Jun 27 '20 at 10:29