将我的功能输出指向另一个功能

时间:2015-03-29 08:39:58

标签: python python-3.x

我的代码:

import re

def matches(line, opendelim='(', closedelim=')'):
    stack = []
    for m in re.finditer(r'[{}{}]'.format(opendelim, closedelim), line):
        pos = m.start()
        if line[pos-1] == '\\':
            continue
        c = line[pos]
        if c == opendelim:
            stack.append(pos+1)
        elif c == closedelim:
            if len(stack) > 0:
                prevpos = stack.pop()
                yield (line[prevpos:pos], len(stack))
            else:
                print("encountered extraneous closing quote at pos {}: '{}'".format(pos, line[pos:] ))
            pass
    if len(stack) > 0:
        for pos in stack:
             print("expecting closing quote to match open quote starting at: '{}'"
              .format(line[pos-1:]))

line = "f(g_1(a, g_2(a, b, g_3(c)), g_2(g_3(a, b, g_4(a), e))))"
a = str(print(','.join([part for part, level in matches(line) if level == 1])))
print(extract_tokens(a))

当我分别运行这两个函数时它会返回。如何在单个脚本文件中运行输出时获取输出?

1 个答案:

答案 0 :(得分:0)

请注意,print 总是 return None,所以这一行:

a = str(print(','.join([part for part, level in matches(line) if level == 1])))

相当于:

print(...)
a = str(None)

因此当你跑:

print(extract_tokens(a))

你得到['None']作为输出。调用函数时问题是,而是你传递的参数。将print(a)放在一个单独的行上,你应该没问题。