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 #1561

Closed
zsef123 opened this issue Sep 2, 2017 · 8 comments
Closed

Mutable Default Arguments #1561

zsef123 opened this issue Sep 2, 2017 · 8 comments
Labels
bug Issue described a bug difficulty easy Easy issue: required small fix

Comments

@zsef123
Copy link
Contributor

zsef123 commented Sep 2, 2017

Mutable arguments can have unintended behavior.
like calling repeatedly under function

def foo(x=[]):
    x.append(1)
    return x

if call foo 3 times, the result is [1,1,1] Python Reference, The Hitchhiker’s Guide to Python

In summerization/graph.py:175

def add_node(self, node, attrs=None):
        if attrs is None:
            attrs = []```

def add_edge(self, edge, wt=1, label='', attrs=[]):

add_edge function is used [] in default argument but add_node function is used None.
And then check if attrs is None then assigns empty list

I think way of add_node is more secure than add_edge.

How about changing [] to None?

@poornagurram
Copy link
Contributor

Repeated calls will not change default argument if it is none. It's more secure than [].

@piskvorky piskvorky added bug Issue described a bug difficulty easy Easy issue: required small fix labels Sep 2, 2017
@piskvorky
Copy link
Owner

piskvorky commented Sep 2, 2017

@zsef123 @poornagurram I'm not familiar with that code base, but it looks like a bug at a glance. Thanks for catching this!

Can you send a PR with a fix? Any other places with unintended mutable defaults?

@menshikh-iv
Copy link
Contributor

Resolved in #1562

@gojomo
Copy link
Collaborator

gojomo commented Sep 5, 2017

Just noticed this; a perhaps better fix (at least in the word2vec/keyedvectors/doc2vec cases I've looked closely at) could be to supply empty tuples as the defaults. (They'd thus be armored against any inadvertent future mutations – but no None-check & assign lines would later be necessary.)

@zsef123
Copy link
Contributor Author

zsef123 commented Sep 5, 2017

@gojomo I'm just following type on original code(list).
Empty tuple sounds nice.
That is more simple than checking None.
I'll check soon

@piskvorky
Copy link
Owner

Same with set vs frozenset (if that came up).

@zsef123
Copy link
Contributor Author

zsef123 commented Sep 6, 2017

@gojomo @piskvorky
In PR #1572 ( I missed test in my fault )

Test failed at keyedvector.py:426, most_similar_cosmul method.

File "/home/travis/build/RaRe-Technologies/gensim/gensim/models/keyedvectors.py", line 476, in most_similar_cosmul
all_words = set([self.vocab[word].index for word in positive + negative

Once change from None to (), will have to exclude keyedvector class or changed to positive + list(negative)

In case of negative used before reassign(under the code)

negative = [
            self.word_vec(word, use_norm=True) if isinstance(word, string_types) else word
            for word in negative
        ]

So I think should cope with using positive, negative as list.
And I think None is more expose intention.

@gojomo
Copy link
Collaborator

gojomo commented Sep 7, 2017

Or could move all_words initialization to before line where positive is reassigned to be a list. Or could change all_words composition to be …(word in positive) or (word in negative)….

Unsure of issue with use of immutable negative before re-assign; reading the immutable default is fine, only mutation attempts are a concern that we'd like to fail noisily rather than have side-effects on future calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue described a bug difficulty easy Easy issue: required small fix
Projects
None yet
Development

No branches or pull requests

5 participants