如何在多个函数中引用全局变量?

时间:2015-11-14 12:07:27

标签: python

我在一个简单的单词频率计数程序中遇到了全局变量引用问题。我检查了herehere以及python docs的答案。但是,我仍然没有得到全局变量引用的想法。

from collections import Counter

with open('c:/Users/Nick/Downloads/sample_file.txt') as f:
    words = f.read().lower().split()

c = Counter(words)

total_words = sum(c.values())


def top_n_words(n):
    global c
    # c = Counter(words)
    top_n = c.most_common(n)
    print("Top %d words are:" % n)
    print("-" * 20)
    for w, c in top_n:
        # print("%10s: %-10s" % (w, c))
        print("{word:>10s}: {counts:<10d}".format(word=w, counts=c))


def word_appears(w):
    # global c
    c = Counter(words)
    print("The word '{word:s}' appears {time:d} times.".format(word = w, time = c[w]))


top_n_words(12)
print("-" * 20)
print("Total words: %d" % total_words)
print("Total words: {t:d}".format(t=sum(c.values())))
word_appears("history")
  1. top_n_words函数中,我已声明c是全局的。我应该在word_appears函数中声明它是全局的吗?它没有用。
  2. 为什么我无法引用c函数中的printtop_n_wordsword_appears的顺序是否会影响最终的打印功能?
  3. 处理这种情况的好习惯是什么?

1 个答案:

答案 0 :(得分:1)

将所有代码放在函数中,并仅使用参数使计数器可用。例如:

from collections import Counter


def read_words(file_name):
    with open(file_name) as f:
        words = f.read().lower().split()
    return words


def top_n_words(counter, n):
    top_n = counter.most_common(n)
    print("Top %d words are:" % n)
    print("-" * 20)
    for w, c in top_n:
        # print("%10s: %-10s" % (w, c))
        print("{word:>10s}: {counts:<10d}".format(word=w, counts=c))


def word_appears(counter, w):
    print("The word '{word:s}' appears {time:d} times.".format(word=w,
        time=counter[w]))


if __name__ == '__main__':

    def main(file_name):
        words = read_words(file_name)
        counter = Counter(words)
        top_n_words(counter, 12)
        print("-" * 20)
        print("Total words: %d" % len(words))
        word_appears(counter, "history")

    main('words.txt')