code: def deep_setattr(obj, key, val):
      """
      Set an attribute `key` on the object. If any of the prefix attributes do
      not exist, they are set to :class:`~pyro.nn.PyroModule`.
      """
  
      def _getattr(obj, attr):
          obj_next = getattr(obj, attr, None)
          if obj_next is not None:
              return obj_next
          setattr(obj, attr, PyroModule())
          return getattr(obj, attr)
  
      lpart, _, rpart = key.rpartition(".")
      # Recursive getattr while setting any prefix attributes to PyroModule
      if lpart:
          obj = functools.reduce(_getattr, [obj] + lpart.split("."))
      setattr(obj, rpart, val)
  
file_path: pyro/pyro/infer/autoguide/utils.py
imports: import functools
  from contextlib import contextmanager
  from pyro import poutine
  from pyro.nn.module import PyroModule
label_desc: Use image node_id 3 for calling a global function with example usage: deep_setattr(obj, key, val) and returns: getattr, obj_next