Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutable default arguments and potential bugs in the future #107

Open
jparas-3 opened this issue Dec 21, 2022 · 1 comment
Open

Mutable default arguments and potential bugs in the future #107

jparas-3 opened this issue Dec 21, 2022 · 1 comment

Comments

@jparas-3
Copy link

There are instances where mutable default arguments (empty lists/dicts) are used in function/method definitions, such as in amptorch.trainer.Atoms.init, which has a config default value of an empty dict. You should never use mutable default arguments in function definitions, since every call to that function that uses the default argument will share that same list/dict, which can cause some weird bugs.

The safe alternative is to set the default value to None, check if the argument's value is None, and then assign it accordingly.

For the specific init example, I think a better alternative would just be to get rid of the default value entirely in order to enforce the config as an argument. Not sure where else this issue pops up, but it should be easy to find all instances of '=[]' and '={}' using grep. This is potentially an issue in other medford-group repos as well.

@jparas-3 jparas-3 changed the title Mutable default arguments Mutable default arguments and potential bugs in the future Dec 21, 2022
@ajmedford
Copy link
Collaborator

Hi Jacob,

Thanks for pointing this out. I don't expect that changing this would break anything, although @mshuaibii or @nicoleyghu can confirm. If you (or anyone else) has time to make the changes and submit a PR that would be great!

Here is an article detailing the issue for reference: https://docs.python-guide.org/writing/gotchas/

Best,
AJ

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

No branches or pull requests

2 participants