Returns a copy of the graph. The copy method by default returns an independent shallow copy of the graph and attributes. That is, if an attribute is a container, that container is shared by the original an the copy. Use Python's `copy.deepcopy` for new containers.
(self, as_view=False)
| 1590 | return False |
| 1591 | |
| 1592 | def copy(self, as_view=False): |
| 1593 | """Returns a copy of the graph. |
| 1594 | |
| 1595 | The copy method by default returns an independent shallow copy |
| 1596 | of the graph and attributes. That is, if an attribute is a |
| 1597 | container, that container is shared by the original an the copy. |
| 1598 | Use Python's `copy.deepcopy` for new containers. |
| 1599 | |
| 1600 | If `as_view` is True then a view is returned instead of a copy. |
| 1601 | |
| 1602 | Notes |
| 1603 | ----- |
| 1604 | All copies reproduce the graph structure, but data attributes |
| 1605 | may be handled in different ways. There are four types of copies |
| 1606 | of a graph that people might want. |
| 1607 | |
| 1608 | Deepcopy -- A "deepcopy" copies the graph structure as well as |
| 1609 | all data attributes and any objects they might contain. |
| 1610 | The entire graph object is new so that changes in the copy |
| 1611 | do not affect the original object. (see Python's copy.deepcopy) |
| 1612 | |
| 1613 | Data Reference (Shallow) -- For a shallow copy the graph structure |
| 1614 | is copied but the edge, node and graph attribute dicts are |
| 1615 | references to those in the original graph. This saves |
| 1616 | time and memory but could cause confusion if you change an attribute |
| 1617 | in one graph and it changes the attribute in the other. |
| 1618 | NetworkX does not provide this level of shallow copy. |
| 1619 | |
| 1620 | Independent Shallow -- This copy creates new independent attribute |
| 1621 | dicts and then does a shallow copy of the attributes. That is, any |
| 1622 | attributes that are containers are shared between the new graph |
| 1623 | and the original. This is exactly what `dict.copy()` provides. |
| 1624 | You can obtain this style copy using: |
| 1625 | |
| 1626 | >>> G = nx.path_graph(5) |
| 1627 | >>> H = G.copy() |
| 1628 | >>> H = G.copy(as_view=False) |
| 1629 | >>> H = nx.Graph(G) |
| 1630 | >>> H = G.__class__(G) |
| 1631 | |
| 1632 | Fresh Data -- For fresh data, the graph structure is copied while |
| 1633 | new empty data attribute dicts are created. The resulting graph |
| 1634 | is independent of the original and it has no edge, node or graph |
| 1635 | attributes. Fresh copies are not enabled. Instead use: |
| 1636 | |
| 1637 | >>> H = G.__class__() |
| 1638 | >>> H.add_nodes_from(G) |
| 1639 | >>> H.add_edges_from(G.edges) |
| 1640 | |
| 1641 | View -- Inspired by dict-views, graph-views act like read-only |
| 1642 | versions of the original graph, providing a copy of the original |
| 1643 | structure without requiring any memory for copying the information. |
| 1644 | |
| 1645 | See the Python copy module for more information on shallow |
| 1646 | and deep copies, https://docs.python.org/3/library/copy.html. |
| 1647 | |
| 1648 | Parameters |
| 1649 | ---------- |