diff options
author | Dan Schult <dschult@colgate.edu> | 2022-08-01 07:44:15 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-01 15:44:15 +0400 |
commit | 7ff2383f180989aad98743eb705063958fc86c23 (patch) | |
tree | dc3ce2cafbaffd8a56f9bf90cdc462ab3951c683 /networkx/relabel.py | |
parent | 28f78cfa9a386620ee1179582fda1db5ffc59f84 (diff) | |
download | networkx-7ff2383f180989aad98743eb705063958fc86c23.tar.gz |
Allow classes to relabel nodes -- casting (#5903)
Diffstat (limited to 'networkx/relabel.py')
-rw-r--r-- | networkx/relabel.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/networkx/relabel.py b/networkx/relabel.py index 35e71536..65297d57 100644 --- a/networkx/relabel.py +++ b/networkx/relabel.py @@ -114,9 +114,13 @@ def relabel_nodes(G, mapping, copy=True): -------- convert_node_labels_to_integers """ - # you can pass a function f(old_label)->new_label + # you can pass a function f(old_label) -> new_label + # or a class e.g. str(old_label) -> new_label # but we'll just make a dictionary here regardless - if not hasattr(mapping, "__getitem__"): + # To allow classes, we check if __getitem__ is a bound method using __self__ + if not ( + hasattr(mapping, "__getitem__") and hasattr(mapping.__getitem__, "__self__") + ): m = {n: mapping(n) for n in G} else: m = mapping |