Auto increment identifiers for class instances

Disclaimer This is for the semester project I'm working on right now. My question relates to detailing the level of implementation and is not part of the assessment framework. I am only writing this code as a way to test the theory that I am proposing for the paper I will write.

In addition, I have enjoyed reviewing the answers to this question , so please do not view this as a duplicate of this question

Problem :

I have a graph (G = (V, E)). At some point in my algorithm, I need to turn this into a hypergraph (in a sense) by “squeezing” several nodes (for example, v_1, v_2, ..., v_n ) into one node (say, v ). In the context of the problem, this means that I need to change the edges in E so that any edge of E between any of v_1, v_2, v_n and any other node u in v changed so that E now between u and v .

To fix that between any pair of nodes several different edges can exist, I need to make a unique identifier for each edge. I tried to do this using and ID, which at the moment I can not implement correctly.

This is what I tried :

 class Edge: _ID = 0 def __init__(self, u, v, w, c,f=0): self.id = Edge._ID Edge._ID += 1 self.src = u self.dest = v self.weight = w self.capacity = c self.flow = f 

However, when I try to instantiate a new edge, I get the following error:

 >>> e = Edge(1,3,5,10,0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "UnsplittableFlow.py", line 14, in __init__ self.id = Edge._ID; Edge._ID += 1 UnboundLocalError: local variable '_ID' referenced before assignment 

EDIT

With suggestions for some answers, I was able to fix the instant-time error. However, another error persists. Here is my code and errors:

 class Edge: _ID = 0 def __init__(self, u, v, w, c,f=0): self.id = self._ID; self._ID += 1 self.src = u self.dest = v self.weight = w self.capacity = c self.flow = f 

Error:

 >>> e = Edge(1,3,5,10,0) >>> e.id 0 >>> Edge._ID 0 >>> f = Edge(2,3,5,10,0) >>> f.id 0 >>> Edge._ID 0 

I would be grateful for any help

thanks

+2
source share
4 answers

You can use self to access _ID .

 self.id = self._ID self.__class__._ID += 1 

If you are using CPython, you may have a lazy person id:

 class Edge(object): @property def id(self): return id(self) 
+2
source

Your edited code processes _ID as if it were an instance variable, not a class variable. Based on Matt Joiner, answer what I think you mean with the following:

 class Edge: _ID = 0 def __init__(self, u, v, w, c,f=0): self.id = self._ID; self.__class__._ID += 1 self.src = u self.dest = v self.weight = w self.capacity = c self.flow = f 

When I run your examples with this Edge definition, I get:

 >>> e = Edge(1,3,5,10,0) >>> e.id 0 >>> Edge._ID 1 >>> f = Edge(2,3,5,10,0) >>> f.id 1 >>> Edge._ID 2 

What is the desired result. However, others have noted that your source code worked for them, just like this code works for me, so I suspect that the real problem is somewhere else in your code.

+4
source

Before creating an instance of any Edge, you can explicitly set a class variable of 0:

 Edge._ID = 0 e = Edge(1,3,5,10,0) f = Edge(2,3,4,5,0) 

And the identifier will be set correctly.

+2
source

While the other suggested answers answer the question asked (which is why I do not accept the one I initially accepted), the correct way would be to use itertools.count as follows:

 class Edge: _ID = itertools.count() def __init__(self, u, v, w, c,f=0): self.id = self._ID.next() 
+1
source

Source: https://habr.com/ru/post/1445638/


All Articles