Skip to content

dot-dsl: remove mutable default arguments#1838

Merged
cmccandless merged 1 commit into
exercism:masterfrom
pranasziaukas:improvement/dot-dsl
Jul 10, 2019
Merged

dot-dsl: remove mutable default arguments#1838
cmccandless merged 1 commit into
exercism:masterfrom
pranasziaukas:improvement/dot-dsl

Conversation

@pranasziaukas
Copy link
Copy Markdown
Contributor

Unintentional mutable default arguments lead to surprising results and are confusing to newcomers to say the least ( https://docs.python-guide.org/writing/gotchas ).

A mutable default argument

def __init__(self, attrs={}):
    self.attrs = attrs

can be replaced with

def __init__(self, attrs=None):
    self.attrs = {} if attrs is None else attrs

or possibly even more compact

def __init__(self, attrs=None):
    self.attrs = attrs or {}

However, this particular exercise forces all of the arguments to be passed through the test suite (specifically, test_malformed_EDGE). In that case, I think it makes sense not to provide any defaults for the constructors of Node and Edge at all, and an immutable default (or, again, no default at all) for the constructor of Graph.

@pranasziaukas pranasziaukas requested a review from a team as a code owner July 10, 2019 11:39
Copy link
Copy Markdown
Contributor

@yawpitch yawpitch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that we should remove the usage of mutable defaults. Also agreed that it's sensible to not provide default values for the Edge and Node classes, as they're dependent on Graph, which is the entry-point interface. Because Graph is the entry-point I think it's reasonable to let it have the default so that an empty Graph can be created.

@yawpitch
Copy link
Copy Markdown
Contributor

@cmccandless any thoughts on this one before I merge?

Copy link
Copy Markdown
Contributor

@cmccandless cmccandless left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cmccandless cmccandless merged commit 42a039d into exercism:master Jul 10, 2019
@pranasziaukas pranasziaukas deleted the improvement/dot-dsl branch July 10, 2019 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants