MCPcopy Create free account
hub / github.com/ipython/ipython / del_var

Method del_var

IPython/core/interactiveshell.py:1466–1509  ·  view source on GitHub ↗

Delete a variable from the various namespaces, so that, as far as possible, we're not keeping any hidden references to it. Parameters ---------- varname : str The name of the variable to delete. by_name : bool If True, delete variables

(self, varname, by_name=False)

Source from the content-addressed store, hash-verified

1464 self.clear_main_mod_cache()
1465
1466 def del_var(self, varname, by_name=False):
1467 """Delete a variable from the various namespaces, so that, as
1468 far as possible, we're not keeping any hidden references to it.
1469
1470 Parameters
1471 ----------
1472 varname : str
1473 The name of the variable to delete.
1474 by_name : bool
1475 If True, delete variables with the given name in each
1476 namespace. If False (default), find the variable in the user
1477 namespace, and delete references to it.
1478 """
1479 if varname in ('__builtin__', '__builtins__'):
1480 raise ValueError("Refusing to delete %s" % varname)
1481
1482 ns_refs = self.all_ns_refs
1483
1484 if by_name: # Delete by name
1485 for ns in ns_refs:
1486 try:
1487 del ns[varname]
1488 except KeyError:
1489 pass
1490 else: # Delete by object
1491 try:
1492 obj = self.user_ns[varname]
1493 except KeyError:
1494 raise NameError("name '%s' is not defined" % varname)
1495 # Also check in output history
1496 ns_refs.append(self.history_manager.output_hist)
1497 for ns in ns_refs:
1498 to_delete = [n for n, o in ns.items() if o is obj]
1499 for name in to_delete:
1500 del ns[name]
1501
1502 # Ensure it is removed from the last execution result
1503 if self.last_execution_result.result is obj:
1504 self.last_execution_result = None
1505
1506 # displayhook keeps extra references, but not in a dictionary
1507 for name in ('_', '__', '___'):
1508 if getattr(self.displayhook, name) is obj:
1509 setattr(self.displayhook, name, None)
1510
1511 def reset_selective(self, regex=None):
1512 """Clear selective variables from internal namespaces based on a

Callers 1

xdelMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected