Giving unique IDs to all nodes?

前端 未结 4 959
小蘑菇
小蘑菇 2021-01-26 18:54

I am making a class in Python that relates a lot of nodes and edges together. I also have other operations that can take two separate objects and merge them into a single object

4条回答
  •  清歌不尽
    2021-01-26 19:51

    You could keep a class variable and use it for ordinal ids:

    class Node(object):
        _id = 0
    
        def __init__(self):
            self._id = Node._id
            Node._id += 1
    

    It also has the benefit that your class will be able to know how many objects were altogether created.

    This is also way cheaper than random ids.

提交回复
热议问题