2011-09-29 147 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.