[英]How to instantiate and extend Python igraph Vertex class
I'd like to extend the igraph Vertex class with some extra variables methods I need for a simulation study.我想用一些额外的变量方法来扩展 igraph Vertex class 进行模拟研究。 However, when I try to import and extend the vertex class, I get an error.
但是,当我尝试导入和扩展顶点 class 时,出现错误。
from igraph import Vertex
v = Vertex()
TypeError: cannot create 'igraph.Vertex' instances
TypeError:无法创建“igraph.Vertex”实例
Likewise, the same error occurs when I try to extend the class.同样,当我尝试扩展 class 时,也会出现同样的错误。
from igraph import Vertex
class MyVertex(Vertex):
def __init__(self):
super().__init__()
my_vertex = MyVertex()
TypeError: cannot create 'MyVertex' instances
TypeError:无法创建“MyVertex”实例
How can I instantiate igraph vertices and extend the igraph Vertex class?如何实例化 igraph 顶点并扩展 igraph 顶点 class?
Vertex
instances are not meant to be instantiated; Vertex
实例并不意味着被实例化; this is intentional.这是故意的。 A
Vertex
is simply a wrapper around a reference to a graph and a vertex index, and it provides a few convenience methods that are usually just proxies to the same method on the associated graph instance. Vertex
只是对图的引用和顶点索引的包装,它提供了一些方便的方法,这些方法通常只是关联图实例上相同方法的代理。 Even if you created your own Vertex
subclass, there is currently no way to tell an igraph Graph
object to start returning your Vertex
subclass for expressions like graph.vs[x]
instead of Vertex
itself.即使您创建了自己的
Vertex
子类,目前也无法告诉 igraph Graph
object 开始返回您的Vertex
子类,以用于graph.vs[x]
等表达式而不是Vertex
本身。
If you want to create your own vertex-like classes, you can try composition instead of inheritance, ie you create a MyVertex
class that takes a Vertex
as its constructor argument, stores the vertex and then forwards method calls to it, depending on what you need to support in your own MyVertex
instance:如果您想创建自己的类顶点类,您可以尝试组合而不是 inheritance,即创建一个
MyVertex
class,它将Vertex
作为其构造函数参数,存储顶点,然后将方法调用转发给它,具体取决于您需要在您自己的MyVertex
实例中支持:
class MyVertex:
def __init__(self, v):
self._v = v
def degree(self, *args, **kwds):
return self._v.degree(*args, **kwds)
...
Note that since Vertex
instances are simply wrappers for a graph and a vertex ID, things can get confusing if you remove vertices from a graph because the remaining vertices are re-indexed by the underlying C library to ensure that their IDs are always in the range [0;请注意,由于
Vertex
实例只是图和顶点 ID 的包装器,如果从图中删除顶点,事情可能会变得混乱,因为剩余的顶点由底层 C 库重新索引以确保它们的 ID 始终在范围内[0; n-1] for n vertices. n-1] 用于 n 个顶点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.