#14 Warn about mutable default function args

closed
nobody
None
5
2002-08-24
2002-05-05
No

How about pychecker giving a warning if you use mutable
default function arguments? Or at least emitting a
warning with a default function argyument of a null
list or dict.

Maybe I'm just dense, but I had written almost 12k
lines of Python code before I tripped over this one.
Pychecker didn't even emit a warning.

Thanks,
Andy L.

Discussion

  • Eric C. Newton

    Eric C. Newton - 2002-05-06

    Logged In: YES
    user_id=18524

    The following:

    def f(x=[]):
    return x.append(None)

    yields this warning:

    test.py:2: Modifying parameter (x) with a default value
    may have unexpected consequences

    There are instances where the use of a mutable default
    argument is quite nice, such as when the values are read-only.

     
  • Neal Norwitz

    Neal Norwitz - 2002-08-24
    • status: open --> closed
     
  • Neal Norwitz

    Neal Norwitz - 2002-08-24

    Logged In: YES
    user_id=33168

    This is implemented for builtin types at least. If the
    problem shows up with current pychecker (0.8.11+), please
    open a bug report.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks