开发者

Import troubles

开发者 https://www.devze.com 2023-04-08 22:49 出处:网络
Here is part of my module:gm.py def avg_list(list): sum = 0 for num in list: sum += num avg = float(sum)/len(list)

Here is part of my module: 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

When I save this as 'gm.py' and open a new script page to input the following function:

import gm
def stats(list):

    s开发者_高级运维tats = {}  # 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

When I run this program and type stats([2,3,4,5,6])... I get an error saying global variable avg_list not defined. I'm not sure if I'm doing the import correctly? Do I need to do something like... from gm import avg_list() ?


Either reference the functions on the module object:

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

or import the functions directly into the global namespace:

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

Note that you should not name a variable list. This masks the built-in list() function / type and can cause confusing errors later if you need to use it.

You can write

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

as

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

I think you should look at the first branch of your median function. Does the list need to be sorted there too, like in the second branch?

Your avg_list function is also masking a built-in function, sum(), which you could use here instead of manually adding:

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

Finally, look at the last line of that function -- it's printing the avg, but stats is expecting it to return the avg. The two aren't the same.


You need to put the module name first (gm.avg_list() and gm.median()) like so:

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

Some Reference links and more info:

PEP 8 - Style Guide for Python Code

A guide to Python Namespaces on the differences between from blah import foo and import 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号