在python脚本中调用函数然后检查条件

时间:2014-05-23 15:57:53

标签: python function functional-programming

我有这个功能:

def ContentFunc():
        storage = StringIO()
        c = pycurl.Curl()
        c.setopt(c.URL, url)
        c.setopt(c.WRITEFUNCTION, storage.write)
        c.perform()
        c.close()
        content = storage.getvalue()


while True:
        ContentFunc()
        if "word" in content:
             out = open('/tmp/test', 'a+')

我想从content追加content = storage.getvalue()。但是不起作用。

错误:

NameError: name 'content' is not defined

你能帮助我吗?

1 个答案:

答案 0 :(得分:4)

在你的功能中

def ContentFunc():
    ...
    content = storage.getvalue()

这定义了该函数范围内的content 。然后该函数结束,并丢弃该名称(以及分配给它的对象)。而是来自函数的return

def ContentFunc():
    ...
    return storage.getvalue()

并在调用函数中指定名称:

content = ContentFunc()