被困在repr犠功能,没有按照我的理解工作

2024-09-27 09:24:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个代码,可以创建一个带有节点的图,并且可以跟踪边。这段代码本身似乎运行得很好,但我无法让repr重写以我期望的方式工作,在阅读了一些strvsreporepr()返回的非字符串线程之后,我似乎无法找出问题的原因。在

!/usr/bin/python
class Edge:
    def __init__(self, here, there, txt):
        self.description = txt     # why am i making this jump?
        self.here = here    # where do i start
        self.there = there   # where to i end
        self.here.out += [self]  # btw, tell here that they can go there

    def __repr__(self):
        return "E(" + self.here.name + " > " + self.there.name + ")"


class Node:
    end = "."
    start = "!"

    def __init__(self, g, nid, name, stop=False, start=False):
        self.nid = nid
        self.graph = g  # where do i live?
        self.name = name  # what is my name?
        self.description = ""  # tell me about myself
        self.stop = stop  # am i a stop node?
        self.start = start  # am i a start node?
        self.out = []  # where do i connect to

    def also(self, txt):
        """adds text to description"""
        sep = "\n" if self.description else ""
        self.description += sep + txt

    def __repr__(self):
        y = "N( :id " + self.nid + \
            "\n   :name " + self.name + \
            "\n   :about '" + self.description + "'" + \
            "\n   :out   " + ",".join(str(n) for n in self.out) + " )"


class Graph:
    def __init__(self):
        self.nodes = []  # nodes, stored in creation order
        self.keys = {}  # nodes indexed by name
        self.m = None  # adjacency matrix
        self.mPrime = None  # transitive closure matrix

    def __repr__(self):
        return "\n".join(str(u) for u in self.nodes)

    def node(self, name):
        """returns a old node from cache or a new node"""
        if not name in self.keys:
            self.keys[name] = self.newNode(name)
        return self.keys[name]

    def newNode(self, name):
        """ create a new node"""
        nid = len(self.nodes)
        tmp = Node(self, nid, name)
        tmp.start = Node.start in name
        tmp.end = Node.end in name
        self.nodes += [tmp]
        return tmp

我的理解是repr每当字符串处理程序调用它的函数时,它都会返回一个字符串。更具体地说,它以字符串的形式引用和实例,查找strstr指向repr并将其作为字符串返回到其调用方法。所以对于我的程序,我有一个图形,它创建得很好,但是当我打印它时,它会卡住连接它所连接的列表,以int而不是字符串的形式返回它?在

我试着把不同的部分转换成一个字符串,我想可能没有调用repr因为它必须显式地转换为一个字符串才能正常工作,还有一些其他的事情。简而言之,我的repr没有被“\n”.join()正确处理,并且不知何故从",".join(str(n) for n in self.out)中获得一个int值,这告诉我它不能连接str和int

我真的不知所措,我读了很多关于repr的google答案和文本,还有很多关于这里的答案,但我没有弄清楚我到底忽略了什么。在

下面是如何构建图形本身,它在stagetext中读取,其中第一行是节点名,出口以>;开头,中间的所有内容都是对节点位置的描述。在

这个图是我应该编辑的,它应该简单地用print graph("mystageText.txt")打印,尽管我已经见过大多数使用repr(graph("myStageText.txt")的repr。因此,我对这件事感到非常困惑,因为没有一个清晰简洁的示例来说明repr以及实例列表。在

提前感谢您的帮助,希望这对StackOverflow来说足够简洁。 抱歉,如果我在试图彻底解决我的问题时有点太罗嗦了。在


Tags: 字符串nameinselftxtnodeheredef
1条回答
网友
1楼 · 发布于 2024-09-27 09:24:21

看来你的问题真的是

        y = "N( :id " + self.nid + \
        #                   ^

self.nid是一个int

另一个错误:Node.__repr__没有返回任何内容。在

相关问题 更多 >

    热门问题