Skip to content

Conversation

@Mahi
Copy link
Contributor

@Mahi Mahi commented Sep 23, 2016

Updated Delay to use normal arguments for args and kwargs instead of *args and **kwargs for the arguments that get forwarded to the delayed callback. Also changed cancel_on_map_end to a keyword argument.

def __init__(
self, delay, callback, cancel_on_map_end=False, *args, **kwargs
):
def __init__(self, delay, callback, cancel_on_map_end=False, args=(), kwargs={}):
Copy link
Member

Choose a reason for hiding this comment

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

I have already made most of the changes and pushed them, though I did miss the doc strings. However, you should never declare default values for arguments as mutable types. I guess the tuple one is fine, but I used None in my implementation. But a dictionary should never be used, since it can be updated and cause issues.

Copy link
Contributor Author

@Mahi Mahi Sep 23, 2016

Choose a reason for hiding this comment

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

This is only true if I store the value and its mutating, in my case I never store it so it's fine. I find it more clear to use a dictionary directly than None, not that there's anything wrong with None either.

Edit: To add, I would do self.kwargs = dict(kwargs) regardless of the default argument to guarantee that it'll be a dictionary. So if I'm doing it anyways, why not use a dict as the argument's value while I'm at it.

Copy link
Member

Choose a reason for hiding this comment

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

It's still very bad practice to do so, and will easily lead other people who read your code down the wrong path.

Copy link
Contributor Author

@Mahi Mahi Sep 23, 2016

Choose a reason for hiding this comment

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

Why is it a bad practice though? I do agree that it's a minor downside that someone else might copy paste my code and thus have a bug in their code, but I'm not going to write all my code based on what someone else might copy paste from it.

Copy link
Member

Choose a reason for hiding this comment

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

You do realize that threading itself uses this better practice, right?

https://github.com/python/cpython/blob/master/Lib/threading.py#L758

https://github.com/python/cpython/blob/master/Lib/threading.py#L781#L782

https://github.com/python/cpython/blob/master/Lib/threading.py#L786

python/cpython@6fe467c

Code is more often read than written. Someone could very easily mistake your implementation and it would throw them off.

Copy link
Contributor Author

@Mahi Mahi Sep 23, 2016

Choose a reason for hiding this comment

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

"Better practice" as in your favourite? ^^ I do realize that threading uses kwargs=None, and there's absolutely nothing wrong with that approach! I even said it's fine:

I find it more clear to use a dictionary directly than None, not that there's anything wrong with None either.

Tbh you're just claiming that it's "better" practice and my practice is bad, yet you have no arguments other than "someone might get confused"... Someone could very easily mistake any implementation, mine works and is perfectly valid so it's just a matter of opinion. I do know that None is used more often, but it's highly due to not having to convert the arguments afterwards in the method with self.x = dict(x). It's used more often, but it's not the better approach.

Copy link
Member

Choose a reason for hiding this comment

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

You should probably do some research then, cause EVERY instance I have found with Google searching points to this. And I have done a lot of searching on it in the past. This argument is getting really dumb and unnecessary...

Copy link
Member

Choose a reason for hiding this comment

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

This is just one of many examples of searching for "python dangerous default value", which is what mutable types as argument defaults are called, returns.
http://stackoverflow.com/questions/9526465/best-practice-for-setting-the-default-value-of-a-parameter-thats-supposed-to-be

@satoon101
Copy link
Member

This request is no longer needed as the changes have already been made. Thank you, though, for the effort.

@satoon101 satoon101 closed this Sep 23, 2016
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.

2 participants