处理函数间状态字符串更新的Python方法?

处理函数间状态字符串更新的Python方法?,python,Python,这似乎是一个补救的话题,但我有点不确定如何处理这个问题。我想到的每一个解决方案似乎都很混乱 我正在使用一些代码,这些代码在执行多个操作时构建消息,然后最终返回带有http响应的消息。现在看起来有点像这样: try: pdict = parser.parseProtocol(id) msg = "parsing worked" except: msg = "parsing failed" try: file = parser.getFile(pdict['file

这似乎是一个补救的话题,但我有点不确定如何处理这个问题。我想到的每一个解决方案似乎都很混乱

我正在使用一些代码,这些代码在执行多个操作时构建消息,然后最终返回带有http响应的消息。现在看起来有点像这样:

try:
    pdict = parser.parseProtocol(id)
    msg = "parsing worked"
except:
    msg = "parsing failed"

try:
    file = parser.getFile(pdict['filePath'])
    msg += "file retrieved"
except:
    msg += "file not found"
假设我想将代码封装到函数中。我怎么能有一条消息在整个过程中得到更新?字符串是不可变的,所以我不能将它们传递给函数并修改它们。一个超级丑陋的解决方案是:

(pdict, msg) = parseSomething()
if pdict:
    (file, msg) = retrieveFile(pdict, msg)

def parseSomething():
    try:
        pdict = parser.parseProtocol(id)
        return (pdict, "parsing worked")
    except:
        return (None, "parsing failed")

def retrieveFile(pdict, msg)
    try:
        file = parser.getFile(pdict['filePath'])
        return (file, msg + "file retrieved")
    except:
        return (None, msg + "file not found")
超级丑

我可以创建一个消息类,或者使用一个长度为1的列表,这会更漂亮,但仍然不是很像Python,对吗?我想我只是希望这些函数获取消息字符串并修改它,而不必返回它,但是字符串是不可变的,所以这不是默认行为


必须有一个顺利的方法来做这件事,我只是空白。帮助?

将您的邮件放入一个数组中,四处传递,然后将每个部分附加到其中


在发送消息之前,请执行一个“.join(msg)”。

将消息放入一个数组中,四处传递,然后将每个部分附加到其中


在发送邮件之前,请执行一个“.join(msg)。

是否考虑将邮件放入列表中,并在发送时附加到列表中

messages = []
try:
    pdict = parser.parseProtocol(id)
    messages.append("parsing worked")
except:
    messages.append("parsing failed")

try:
    file = parser.getFile(pdict['filePath'])
    messages.append("file retrieved")
except:
    messages.append("file not found")

print '\n'.join(messages)

如果您的代码路径特别混乱,请考虑将它们嵌入类:

class Tests(object):
    def __init__(self):
        messages = []
        self.pdict = None

    def parsetest(self):
        try:
            self.pdict = parser.parseProtocol(id)
        except:
            self.messages.append("parsing failed")
        else:
            self.messages.append("parsing worked")

    def retrievetest(self):
        if self.pdict is None:
            raise Exception("called retrievetest() before successfully parsing")
        try:
            file = parser.getFile(self.pdict['filePath'])
        except:
            self.messages.append("file not found")
        else:
            self.messages.append("file retrieved")
后来:

tests = Tests()
tests.parsetest()
if condition:
    tests.retrievetest()
print '\n'.join(tests.messages)

考虑把你的信息放在一个列表中,并在你去的时候附加到它

messages = []
try:
    pdict = parser.parseProtocol(id)
    messages.append("parsing worked")
except:
    messages.append("parsing failed")

try:
    file = parser.getFile(pdict['filePath'])
    messages.append("file retrieved")
except:
    messages.append("file not found")

print '\n'.join(messages)

如果您的代码路径特别混乱,请考虑将它们嵌入类:

class Tests(object):
    def __init__(self):
        messages = []
        self.pdict = None

    def parsetest(self):
        try:
            self.pdict = parser.parseProtocol(id)
        except:
            self.messages.append("parsing failed")
        else:
            self.messages.append("parsing worked")

    def retrievetest(self):
        if self.pdict is None:
            raise Exception("called retrievetest() before successfully parsing")
        try:
            file = parser.getFile(self.pdict['filePath'])
        except:
            self.messages.append("file not found")
        else:
            self.messages.append("file retrieved")
后来:

tests = Tests()
tests.parsetest()
if condition:
    tests.retrievetest()
print '\n'.join(tests.messages)

使您的消息成为类的成员,并传递该类的实例


更好的是,将所有这些函数方法放在一个类上,并将消息作为对象的属性。

将消息作为类的成员,并传递该类的实例


更好的是,将所有这些函数方法放在一个类上,并将消息作为对象的一个属性。

您的(file,msg)方法似乎没有那么糟糕。为什么要用这些“pythonic”的东西让您的生活变得困难?我会选择一种代码最少的方式。你的(文件,味精)方法看起来没那么糟糕。为什么要用这些“pythonic”的东西让你的生活变得困难?我会选择代码最少的方式