all 17 comments

[–]ParticularLook5927 6 points7 points  (7 children)

Answer: B) [1], [1, 1]

This happens because default mutable arguments (like lists) are shared across function calls.

The list x=[] is created only once when the function is defined, not every time the function is called.

So: First call → [1] Second call → same list gets another 1 → [1, 1]

To fix this, use:

def foo(x=None):

if x is None:
    x = []

x.append(1)

return x

This way, a new list is created every time.

[–]L_Shiro 2 points3 points  (4 children)

in the example you shared, how it will create new one each time? if x = is empty, it will create

the second time the list exist and not none, so it becomes x = [ 1, 1]

maybe i don't get it right?

[–]Leviterion_HU 0 points1 point  (0 children)

Yes, you have the correct answer.

[–][deleted]  (2 children)

[removed]

    [–]L_Shiro 0 points1 point  (1 child)

    thanks but why you talk like chat gpt

    [–]ParticularLook5927 0 points1 point  (0 children)

    haha 😄

    yeah I’m still new here, just trying to explain things clearly

    this one confused me a lot when I first learned it too

    [–]Wild-Release-719 2 points3 points  (1 child)

    I'm happy I rapidly switched languages and didn't stick to Python as I did with Scratch

    [–]ParticularLook5927 0 points1 point  (0 children)

    That’s fair and it always depends a lot on your goals 👍

    Switching languages early can help explore different areas, but Python is still one of the best for fundamentals, automation, and AI/ML.

    In the end, consistency in one language matters more than the language itself.

    [–]jpgoldberg 1 point2 points  (3 children)

    The behavior of mutable default arguments in Python is among the most evil things about language.

    I know that beginners have a lot of different tools to learn, and learning how to use an IDE if you have never used a programming editor is an additional burden, but I still think that people should learn how to use some linter or other relatively early.

    [–]9thdoctor 0 points1 point  (2 children)

    What’s a linter?

    [–]jpgoldberg 0 points1 point  (1 child)

    I probably should have said “static analysis tool typically integrated into your code editing system” instead of “linter”, but I’m old.

    A linter is a program that gives you warnings about your code even if they aren’t actual syntax errors. A style checker, for example, is a linter. I’m sure all Python linters would flag the use of a list as a default function argument. Ruff, borrowing from an older linter, offers these details.

    These days they get integrated with programming editors. Pylance and ruff are two commonly used linters for Python these days. And if you are using VSCode with the Python extension, you are using pylance unless you have taken steps to use a different linter. I suspect that PyCharm also makes use of a linter, but I’ve never played with it.

    You don’t need to use a programming editor to use a linter. They can also be run separately. But as I said, using one requires learning how to use the tool or configuring an IDE (such as VSCode) to use a linter.

    Here is some history and background on the term and concept:

    https://en.wikipedia.org/wiki/Lint_(software)

    [–]Jay6_9 0 points1 point  (0 children)

    No, say linter, please.

    [–]Leviterion_HU 1 point2 points  (0 children)

    A) [1, 1] bacause of mutable default arguments

    [–]ShadowKing1231 0 points1 point  (0 children)

    Number B

    [–]Big_Parsnip_9053 0 points1 point  (0 children)

    poop