🔍 Code Extractor

function is_param_method

Maturity: 49

Checks whether an object is a method on a Parameterized object, with optional verification of parameter dependencies.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1529 - 1547
Complexity:
simple

Purpose

This function is used to identify methods that belong to param.Parameterized objects, which is useful for introspection and dynamic behavior in parameter-driven applications. It can optionally verify if the method has been decorated with param.depends, indicating it has parameter dependencies that should trigger updates when those parameters change. This is commonly used in reactive programming patterns with the param library.

Source Code

def is_param_method(obj, has_deps=False):
    """Whether the object is a method on a parameterized object.

    Args:
       obj: Object to check
       has_deps (boolean, optional): Check for dependencies
          Whether to also check whether the method has been annotated
          with param.depends

    Returns:
       A boolean value indicating whether the object is a method
       on a Parameterized object and if enabled whether it has any
       dependencies
    """
    parameterized = (inspect.ismethod(obj) and
                     isinstance(get_method_owner(obj), param.Parameterized))
    if parameterized and has_deps:
        return getattr(obj, "_dinfo", {}).get('dependencies')
    return parameterized

Parameters

Name Type Default Kind
obj - - positional_or_keyword
has_deps - False positional_or_keyword

Parameter Details

obj: The object to check. Can be any Python object, but the function specifically tests if it's a method on a param.Parameterized instance. Expected to be a method object when the function returns True.

has_deps: Boolean flag (default: False) that determines whether to perform additional checking for parameter dependencies. When True, the function not only checks if obj is a method on a Parameterized object, but also verifies that the method has been annotated with param.depends decorator and has actual dependencies defined in its _dinfo attribute.

Return Value

Returns a boolean value. When has_deps=False, returns True if obj is a method on a param.Parameterized object, False otherwise. When has_deps=True, returns True only if obj is a method on a Parameterized object AND has dependencies defined (via param.depends decorator), otherwise returns False or the dependencies value itself if it exists.

Dependencies

  • param
  • inspect

Required Imports

import inspect
import param

Usage Example

import param
import inspect

# Assume get_method_owner is defined elsewhere in the module
def get_method_owner(method):
    return getattr(method, '__self__', None)

class MyParameterized(param.Parameterized):
    value = param.Parameter(default=5)
    
    @param.depends('value')
    def compute(self):
        return self.value * 2
    
    def simple_method(self):
        return self.value

obj = MyParameterized()

# Check if compute is a method on a Parameterized object
result1 = is_param_method(obj.compute)
print(result1)  # True

# Check if compute has dependencies
result2 = is_param_method(obj.compute, has_deps=True)
print(result2)  # True or the dependencies list

# Check simple_method without dependencies
result3 = is_param_method(obj.simple_method, has_deps=True)
print(result3)  # False or None

# Check a non-method object
result4 = is_param_method(obj.value)
print(result4)  # False

Best Practices

  • This function depends on get_method_owner being available in the same scope - ensure it's imported or defined before use
  • When has_deps=True, the function checks for a _dinfo attribute with a 'dependencies' key, which is set by the param.depends decorator
  • The function uses inspect.ismethod which only returns True for bound methods, not unbound functions
  • Be aware that the return value when has_deps=True may not always be a boolean - it returns the actual dependencies value from _dinfo
  • This is typically used in framework code for introspection rather than application-level code
  • The function is designed to work with the param library's reactive programming patterns

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function is_int 42.3% similar

    Checks if an object is an integer type, supporting native Python integers, NumPy integer types, and optionally float types with integer values.

    From: /tf/active/vicechatdev/patches/util.py
  • function resolve_dependent_kwargs 41.1% similar

    Resolves parameter dependencies in a dictionary by evaluating dependent parameter values, Parameterized instance methods, and parameterized functions.

    From: /tf/active/vicechatdev/patches/util.py
  • function resolve_dependent_value 40.6% similar

    Recursively resolves parameter dependencies in a value by evaluating parameterized methods, functions, and widgets, including those nested in collections (lists, tuples, dicts, slices).

    From: /tf/active/vicechatdev/patches/util.py
  • function deprecated_opts_signature 38.7% similar

    A utility function that analyzes arguments and keyword arguments to determine if the new or deprecated .opts method signature is being used, returning flags and processed options accordingly.

    From: /tf/active/vicechatdev/patches/util.py
  • function check_dependencies_v1 38.4% similar

    Validates the presence of required Python packages by attempting to import them and returns a list of any missing dependencies.

    From: /tf/active/vicechatdev/email-forwarder/run_service.py
← Back to Browse