类实例的自动递增ID

编程入门 行业动态 更新时间:2024-10-16 18:32:35
本文介绍了类实例的自动递增ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

免责声明:这是我目前正在研究的一个学期项目.我的问题是关于实现级别的细节,而不是评分方案的一部分.我只是在编写此代码,以此来检验我将为我将要写的论文提出的理论.

Disclaimer: This is for a semester project that I am currently working on. My question is regarding an implementation level detail and is not part of the grading scheme. I am only writing this code as a way to test the theory that I am proposing for the paper that I will write.

我还考虑了这个问题的答案运气不好,所以请不要将此视为该问题的重复

Also, I have considered the answers for this question with little luck, so please do not consider this as a duplicate of that question

问题:

我有一个图(G =(V,E)).在算法的某个时刻,我需要通过将多个节点(例如v_1, v_2, ..., v_n)折叠"到一个节点(例如v)来将其转变为超图(在某种意义上).在问题的上下文中,这意味着我需要更改E中的边,以使V中的任何v_1, v_2, v_n与任何其他节点u之间的任何边e都被更改,使得e现在在u和v之间.

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 "collapsing" multiple nodes (say, v_1, v_2, ..., v_n) into one node (say, v). In the context of the problem, this implies that I need to change the edges in E such that any edge e between any of v_1, v_2, v_n and any other node u in V will be changed such that e is now between u and v.

为了捕获现在在任意一对节点之间可能存在多个不同的边缘,我需要为每个边缘创建一个唯一的标识符.我已经尝试通过ID和ID来执行此操作,而我目前无法正确实现.

In order to capture that there may now exist multiple distinct edges between any pair of nodes, I need to make a unique identifier for each edge. I have tried to do this by means of and ID, which I am currently unable to properly implement.

这是我尝试过的:

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

编辑:

有了一些答案的建议,我已经能够解决实例化时间错误.但是,另一个错误仍然存​​在.这是我的代码和错误:

With the suggestions of some answers, I have been able to fix the instantiation-time error. However, another error persists. Here is my code and the 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

错误:

>>> 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

我将不胜感激

谢谢

推荐答案

您仍然可以使用self到达_ID.

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

如果您使用的是CPython,则可以拥有一个懒人的ID:

If you're using CPython, you can have a lazy man's ID:

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

更多推荐

类实例的自动递增ID

本文发布于:2023-11-28 15:11:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!