code: def preserve_namespace(newns=None):
      
      ns = cmds.namespaceInfo(an=True)
      try:
          cmds.namespace(set=newns)
          yield
      finally:
          cmds.namespace(set=ns)
docs: Contextmanager that will restore the current namespace
  
      :param newns: a name of namespace that should be set in the beginning. the original namespace will be restored afterwards.
                    If None, does not set a namespace.
      :type newns: str | None
      :returns: None
      :rtype: None
      :raises: None