canonical_sets.utils.safe_isinstance

safe_isinstance(obj, class_str)[source]

Check isinstance without requiring imports.

Parameters
  • obj (Any) – The object to check.

  • class_str (str) – The class name to check.

Returns

True if the object is an instance of the class.

Return type

bool

Example

>>> model = torch.nn.Module()
>>> safe_isinstance(model, "torch.nn.Module")