hasmethod#

deeptrack.utils.hasmethod(obj: Any, method_name: str) bool#

Check whether an object has a callable method named method_name.

Returns True if the object has a field named method_name that is callable. Otherwise, returns False.

Parameters#

obj: Any

The object to inspect.

method_name: str

The name of the method to look for.

Returns#

bool

True if the object has an attribute named method_name that is callable.

Examples#

>>> from deeptrack.utils import hasmethod

Check if an object has a method called ‘foo’:

>>> class MyClass:
...     def foo(self):
...         return 42
>>> obj = MyClass()
>>> hasmethod(obj, "foo")
True
>>> hasmethod(obj, "bar")
False

Built-in types:

>>> hasmethod([1, 2, 3], "append")
True
>>> hasmethod([1, 2, 3], "not_a_method")
False

Modules:

>>> import math
>>> hasmethod(math, "sqrt")
True
>>> hasmethod(math, "not_existing")
False

Edge cases:

>>> hasmethod(42, "bit_length")
True
>>> hasmethod(42, "foo")
False
>>> hasmethod(None, "foo")
False