Answers for "how to check if a variable holds a class reference in python"

0

how to check if a variable holds a class reference in python

#how to check if a variable holds a class reference, if it is an instance
#or a function or a method in python
import inspect
class cls:
  pass

print(inspect.isclass(cls))
>True
instance = cls()
print(issinstance(instance, cls))
>True
print(inspect.isclass(instance))
>False

#you can also use
#inspect.isfunction(function_name)
#inspect.ismethod(method_name)
Posted by: Guest on February-15-2022

Code answers related to "how to check if a variable holds a class reference in python"

Python Answers by Framework

Browse Popular Code Answers by Language