“cmd.alter” changes atomic properties using an expression evaluated within a temporary namespace for each atom.
You can provide a dictionary into which information can be stored or recalled instead of using the global pymol namespace.
def alter(selection, expression, quiet=1, space=None):