question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default argument value is mutable

See original GitHub issue

Hello, @wengong-jin !

My IDE prevents me that default argument value is mutable (MolTreeNode class). I want to make sure you understand that default argument values are really mutable and in Python we have the following behaviour:

def append_to(element, to=[]):
    to.append(element)
    return to
my_list = append_to(12)
print(my_list)
my_other_list = append_to(42)
print(my_other_list)

Output: [12] [12, 42]

Did you use it as trick? For example, in MolTreeNode class? Also I see the same problem in enum_assemble function in chemutils.py

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Oktai15commented, Jun 20, 2018

@wengong-jin Don’t you think that it can influence on training and, therefore, result? Because your program could be use previous state of list. Can you check that moment?

It refers to create new instance of MolTreeNode and call enum_assemble function. MolTreeNode: super_root = MolTreeNode("") – jtnn_dec.py root = MolTreeNode(self.vocab.get_smiles(root_wid)) – jtnn_dec.py node_y = MolTreeNode(self.vocab.get_smiles(wid)) – jtnn_dec.py node_y = MolTreeNode(self.vocab.get_smiles(next_wid)) – jtnn_dec.py enum_assemble: cands = enum_assemble(self, neighbors) – mol_tree.py cands = enum_assemble(node_x, neighbors) – jtnn_dec.py

Also, this problem may cause of this https://github.com/wengong-jin/icml18-jtnn/issues/9 issue

0reactions
wengong-jincommented, Jun 22, 2018

I don’t think this problem will cause memory issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Mutable Defaults Are The Source of All Evil
In Python, when passing a mutable value as a default argument in a function, the default argument is mutated anytime that value is...
Read more >
Common Gotchas - The Hitchhiker's Guide to Python
Python's default arguments are evaluated once when the function is defined, not each time the function is called (like it is in say,...
Read more >
Tip: Watch out for mutable default arguments in Python
Default arguments in Python are evaluated only once. The evaluation happens when the function is defined, instead of every time the function ...
Read more >
Do not use mutable objects as default arguments in Python
This only applies to mutable objects. If you are using, say, integers as the default values for your arguments, then there is no...
Read more >
1 Python Anti-Pattern - Mutable Default Arguments
Python's default arguments are evaluated once when the function is defined. This means that if you use a mutable default argument and mutate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found