2011-09-29 123 views
1

这里是我的模块的一部分:gm.py进口烦恼

def avg_list(list): 
    sum = 0 
    for num in list: 
     sum += num 

    avg = float(sum)/len(list) 
    print avg 



def median(list): 
    i = len(list) 
    if not i%2: # if i divided my 2 has no remainder 

     return (list[(i/2)-1]+list[i/2])/2.0 # return the value of this block 


    else: 
     median = sorted(list)[len(list)/2] # otherwise, when the list is sorted, the index of len(s)/2 is the middle number. 
     return median 

当我保存这个为“gm.py”,并打开一个新的脚本页面输入以下功能:

import gm 
def stats(list): 

    stats = {} # empty dict named stats 
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values] 
    stats['median'] = median(list) # same for median 
    return stats 

当我运行这个程序并键入stats([2,3,4,5,6])...我得到一个错误,说全局变量avg_list没有定义。我不确定我是否正确地进行导入? 我是否需要从gm import avg_list()中执行类似操作?

回答

4

要么引用模块对象的功能:

import gm 
def stats(list): 

    stats = {} # empty dict named stats 
    stats['average'] = gm.avg_list(list) # Stats(key)[average] = mean of the list of numbers [values] 
    stats['median'] = gm.median(list) # same for median 
    return stats 

或直接导入功能集成到全局命名空间:

from gm import avg_list, median 
def stats(list): 

    stats = {} # empty dict named stats 
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values] 
    stats['median'] = median(list) # same for median 
    return stats 

注意你不应该命名变量list 。这掩盖了内置的list()函数/类型,如果需要使用它,以后可能会导致混淆错误。

你可以写

stats = {} # empty dict named stats 
stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values] 
stats['median'] = median(list) # same for median 
return stats 

stats = {'average': avg_list(list), 'median': median(list)} 
return stats # or just return the dict literal, no real need to give it a name. 

我想你应该看看你median功能的第一家分店。列表是否需要在那里排序,就像在第二个分支中一样?

avg_list功能也掩盖一个内置功能,sum(),你可以在这里使用,而不是手动添加:

def avg_list(list): 
    avg = float(sum(list))/len(list) 
    print avg 

最后,看该函数的最后一行 - 这是print荷兰国际集团avg,但stats预计到returnavg。两者不一样。

+0

如果我添加排序到第一个分支,我收到一个TypeError:'int'对象不可迭代。 –

+0

@ G.G你不能把整个东西都包装在'sorted'中,我的意思是你需要对列表进行排序_before_查找中位数,列表中是否有奇数或偶数条目。尝试在函数的顶部添加'list = sorted(list)'或'list.sort()',并去掉其他调用'sorted'。 – agf

+0

@afg啊,我的错......我可以那样做。我用适当的进口再次运行程序,并回来了: >>> stats([2,5,6,7,8]) 5.6 {'average':None,'median':6} –

2

你需要首先把模块名称(gm.avg_list()gm.median()),像这样:在不同

PEP 8 - Style Guide for Python Code

A guide to Python Namespaces

import gm 
def stats(list): 

    stats = {} # empty dict named stats 
    stats['average'] = gm.avg_list(list) # Stats(key)[average] = mean of the list of numbers [values] 
    stats['median'] = gm.median(list) # same for median 
    return stats 

一些引用链接和更多信息在from blah import fooimport blah之间

  • import SomeModule

This is the simplest way to do imports and generally recommended. You get access to the module’s namespace provided you use the module’s name as a prefix. This means that you can have names in your program which are the same as those in the module, but you’ll be able to use both of them. It’s also helpful when you’re importing a large number of modules as you see which module a particular name belongs to.

  • from SomeModule import SomeName

This imports a name (or a few, separated by commas) from a module’s namespace directly into the program’s. To use the name you imported, you no longer have to use a prefix, just the name directly. This can be useful if you know for certain you’ll only need to use a few names. The downside is that you can’t use the name you imported for something else in your own program. For example, you could use add() instead of Integer.add(), but if your program has an add() function, you’ll lose access to the Integer’s add() function.