class Node:
    def __init__(self, edges = set()):
        self.edges = edges


def main():
    foo = Node()
    bar = Node()
    quz = Node()

    foo.edges.add(bar)
    bar.edges.add(foo)

    assert(foo is not bar) # assertion succeeds
    assert(foo is not quz) # assertion succeeds
    assert(bar is not quz) # assertion succeeds
    assert(len(quz.edges) == 0) # assertion fails??


main()
spoiler

Mutable default values are shared across objects. The set in this case.

  • milkisklim@lemm.ee
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    9 months ago

    What do you mean by trivial? I am not necessarily the most experienced coder, but it does a great job yelling at me to keep methods short and simple.

    I’d suggest taking five minutes whenever and look up the ruff ruleset to see if it would be helpful for you.

    Also maybe because I don’t know how to use pylint in vs code, but the only semi useful thing it catches for me is if my venv doesn’t have a library the code imports.

    Edit: For example, Ruff has caught this bug (mutable argument defaults) in my code before.