You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python模块函数调用报NameError,求助正确实现方案

解决模块函数调用时的NameError问题

问题背景

你把原本运行正常的3D打印模拟代码拆分成模块和外部调用程序后,遇到了NameError,但简化变量名的测试代码却能正常运行,这确实容易让人困惑。先把你的代码和报错信息整理出来:

原可运行代码

def print_models(unprinted_designs, completed_designs):
    """ Simulate printing each design, until none are left.
    Move each design to completed_models after printing. """
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        #Simulate creating a 3D print from the design.
        print("Printing model: " + current_design)
        completed_models.append(current_design)

def show_completed_models(completed_models):
    """Show all the models that were printed."""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

拆分后的模块文件printing_functions.py

def print_models(unprinted_designs, completed_designs):
    """ Simulate printing each design, until none are left.
    Move each design to completed_models after printing. """
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        #Simulate creating a 3D print from the design.
        print("Printing model: " + current_design)
        completed_models.append(current_design)

def show_completed_models(completed_models):
    """Show all the models that were printed."""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

外部调用程序

import printing_functions as pf

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
pf.print_models(unprinted_designs, completed_models)
pf.show_completed_models(completed_models)

报错追踪

Traceback (most recent call last):
  File "/home/pi/Documents/Python Projects/python_work/8-15_printing_models.py", line 6, in 
    pf.print_models(unprinted_designs, completed_models)
  File "/home/pi/Documents/Python Projects/python_work/printing_functions.py", line 11, in print_models
    completed_models.append(current_design)
NameError: name 'completed_models' is not defined

问题根源

这是一个非常典型的参数名与内部使用的变量名不匹配的错误:

  • 你在模块的print_models函数定义里,第二个参数是completed_designs
  • 但在函数内部,你却写了completed_models.append(current_design)——这个completed_models在函数的作用域里根本没有定义,Python找不到它,自然就抛出了NameError。

而你之前的简化版代码(用unprintedcompleted)能正常运行,是因为简化版里参数名和内部使用的变量名是完全一致的,没有出现这种拼写/命名不一致的问题。

修复方案

只需要让函数的参数名和内部使用的变量名保持一致即可,有两种修改方式:

方式一:修改函数参数名(推荐,贴合原代码命名习惯)

把模块里print_models的第二个参数改成completed_models,和内部调用的变量名统一:

def print_models(unprinted_designs, completed_models):  # 修改参数名
    """ Simulate printing each design, until none are left.
    Move each design to completed_models after printing. """
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        #Simulate creating a 3D print from the design.
        print("Printing model: " + current_design)
        completed_models.append(current_design)  # 这里和参数名匹配了

def show_completed_models(completed_models):
    """Show all the models that were printed."""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

方式二:修改函数内部的变量名

保持参数名completed_designs不变,把函数内部的completed_models.append改成completed_designs.append

def print_models(unprinted_designs, completed_designs):
    """ Simulate printing each design, until none are left.
    Move each design to completed_models after printing. """
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        #Simulate creating a 3D print from the design.
        print("Printing model: " + current_design)
        completed_designs.append(current_design)  # 修改这里的变量名

def show_completed_models(completed_models):
    """Show all the models that were printed."""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

两种方式都能解决问题,选你觉得更符合代码命名逻辑的即可。

内容的提问来源于stack exchange,提问作者jcos

火山引擎 最新活动