Ir al contenido

Mutable default argument (ARCH2194)

Esta página aún no está disponible en tu idioma.

Key python.quality.mutable-default-arg · Severity warning · Category quality

Python default arguments are evaluated once at definition time; a mutable default is shared across every call, causing a classic bug. Use None instead.

def add_item(item, bucket=[]):
bucket.append(item)
return bucket
def add_item(item, bucket=None):
if bucket is None:
bucket = []
bucket.append(item)
return bucket

Default to None and create the new mutable object inside the body.