Default argument value is mutable
See original GitHub issueHello, @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:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 callenum_assemble
function. MolTreeNode:super_root = MolTreeNode("")
– jtnn_dec.pyroot = MolTreeNode(self.vocab.get_smiles(root_wid))
– jtnn_dec.pynode_y = MolTreeNode(self.vocab.get_smiles(wid))
– jtnn_dec.pynode_y = MolTreeNode(self.vocab.get_smiles(next_wid))
– jtnn_dec.py enum_assemble:cands = enum_assemble(self, neighbors)
– mol_tree.pycands = enum_assemble(node_x, neighbors)
– jtnn_dec.pyAlso, this problem may cause of this https://github.com/wengong-jin/icml18-jtnn/issues/9 issue
I don’t think this problem will cause memory issues.