You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

参照《Python Crash Course》练习时PyCharm中**extra参数变灰原因求解

问题原因

PyCharm将**extra灰化是因为该参数在函数定义中被声明,但全程没有被调用,属于未使用的冗余参数,IDE给出该提示是为了提醒你存在无效的参数定义。

你与教材示例的核心差异

你误认为代码和教材一致,但实际逻辑有本质区别:

  • 教材示例中**user_info是直接被使用的:函数内部向user_info字典中新增键值对后直接返回该字典,参数是有效调用状态,不会被灰化。
  • 你的代码中完全没有操作extra参数,反而操作了未提前初始化的auto_info变量,除了extra灰化的问题外,代码直接运行还会抛出NameError报错。

修正方案

方案1:直接使用extra参数,和教材逻辑对齐

def cars(manufacturer, modelname, **extra):
    extra['fabriekant'] = manufacturer
    extra['model'] = modelname
    return extra

autoprofiel = cars('Volvo', 'xv40', color='black', cruize_control=True)
print(autoprofiel)

方案2:单独初始化字典,手动调用extra参数

如果你需要单独创建新字典存储信息,要先初始化字典并手动合并extra的内容:

def cars(manufacturer, modelname, **extra):
    auto_info = {}
    auto_info['fabriekant'] = manufacturer
    auto_info['model'] = modelname
    auto_info.update(extra)
    return auto_info

autoprofiel = cars('Volvo', 'xv40', color='black', cruize_control=True)
print(autoprofiel)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 08:15:07