关于 PyCharm 中可变默认参数的警告 [英] Warning about mutable default argument in PyCharm

查看:100
本文介绍了关于 PyCharm 中可变默认参数的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyCharm (Python 3) 编写一个 Python 函数,该函数接受字典作为带有 attachment={} 的参数.

I am using PyCharm (Python 3) to write a Python function which accepts a dictionary as an argument with attachment={}.

def put_object(self, parent_object, connection_name, **data):
    ...

def put_wall_post(self, message, attachment={}, profile_id="me"):
    return self.put_object(profile_id, "feed", message=message, **attachment)

在 IDE 中,attachment={} 为黄色.将鼠标移到它上面会显示警告.

In the IDE, attachment={} is colored yellow. Moving the mouse over it shows a warning.

默认参数值是可变的

此检查检测何时将可变值作为列表或字典在参数的默认值中检测到.

This inspection detects when a mutable value as list or dictionary is detected in a default value for an argument.

默认参数值仅在函数定义时评估一次时间,这意味着修改参数的默认值将影响该函数的所有后续调用.

Default argument values are evaluated only once at function definition time, which means that modifying the default value of the argument will affect all subsequent calls of the function.

这是什么意思,我该如何解决?

What does this mean and how can I resolve it?

推荐答案

如果您不更改可变默认参数"或将其传递到任何可以更改的地方,请忽略该消息,因为没有什么可更改的固定".

If you don't alter the "mutable default argument" or pass it anywhere where it could be altered just ignore the message, because there is nothing to be "fixed".

在您的情况下,您解包(执行隐式复制)可变默认参数" - 这样您就安全了.

In your case you only unpack (which does an implicit copy) the "mutable default argument" - so you're safe.

如果您想删除该警告消息",您可以使用 None 作为默认值,并在 None 时将其设置为 {}:

If you want to "remove that warning message" you could use None as default and set it to {} when it's None:

def put_wall_post(self,message,attachment=None,profile_id="me"):
    if attachment is None:
        attachment = {}

    return self.put_object(profile_id,"feed",message = message,**attachment)

<小时>

只是为了解释这意味着什么":Python 中的某些类型是不可变的(intstr、...)其他类型是可变的(例如 dict, set, list, ...).如果您想更改不可变对象,则会创建另一个对象 - 但如果您更改可变对象,则该对象保持不变,但其内容已更改.


Just to explain the "what it means": Some types in Python are immutable (int, str, ...) others are mutable (like dict, set, list, ...). If you want to change immutable objects another object is created - but if you change mutable objects the object remains the same but it's contents are changed.

棘手的部分是类变量和默认参数在函数加载时创建(并且只有一次),这意味着对可变默认参数"或可变类变量"的任何更改都是永久性的:

The tricky part is that class variables and default arguments are created when the function is loaded (and only once), that means that any changes to a "mutable default argument" or "mutable class variable" are permanent:

def func(key, value, a={}):
    a[key] = value
    return a

>>> print(func('a', 10))  # that's expected
{'a': 10}
>>> print(func('b', 20))  # that could be unexpected
{'b': 20, 'a': 10}

PyCharm 可能会显示此警告,因为很容易意外出错(参见例如 Least Astonishment"和可变默认参数 和所有相关问题).但是,如果您是故意这样做的(可变函数参数默认值的良好用途值?)警告可能很烦人.

PyCharm probably shows this Warning because it's easy to get it wrong by accident (see for example "Least Astonishment" and the Mutable Default Argument and all linked questions). However, if you did it on purpose (Good uses for mutable function argument default values?) the Warning could be annoying.

这篇关于关于 PyCharm 中可变默认参数的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆