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

MAINT: Removed supurious assert in histogram estimators #7193

Merged
merged 1 commit into from
Feb 5, 2016

Conversation

madphysicist
Copy link
Contributor

This assert is superfluous. Both possible usages are already being handled:

  1. The function is nominally private for use by histogram. In this case it is always called in an if isinstance(bins, basestring): block (Line 346 in master)
  2. In the unlikely event that someone calls this function externally (e.g. to get access to the estimator functionality), the except KeyError: block on Line 157 will handle the situation better than the assertion since it will not be optimized away and will actually deliver a useful message.

charris added a commit that referenced this pull request Feb 5, 2016
MAINT: Removed supurious assert in histogram estimators
@charris charris merged commit 4c972e9 into numpy:master Feb 5, 2016
@charris
Copy link
Member

charris commented Feb 5, 2016

Thanks @madphysicist . Should get rid of the try .. except also.

@madphysicist
Copy link
Contributor Author

Which try...except?

@madphysicist madphysicist deleted the extra-assert branch February 5, 2016 18:53
@charris
Copy link
Member

charris commented Feb 5, 2016

Same function around dictionary access. Could check for key and raise proper error.

@charris
Copy link
Member

charris commented Feb 5, 2016

Strictly speaking, the whole function should be removed, the methods made private functions, and the dictionary instantiated on module load.

@madphysicist
Copy link
Contributor Author

Isn't the try...except doing exactly that? Checking for key and raising the proper error? It looks fine to me. I agree that the dictionary can be instantiated on load.

@charris
Copy link
Member

charris commented Feb 5, 2016

Using try ... except for ordinary flow control is ugly and the code is not self documenting like it would be with an explicit key check and the else is a waste of bytes. I expect the the try .. except also consumes excess cpu cycles. Just as assert is mistakenly used for flow control, I think the try .. except is often misused for flow control. It makes sense perhaps for duck typing or errors like missing files or calling a complicated function with error returns, but here there is no need here and checking the key is simpler.

@madphysicist
Copy link
Contributor Author

Just so I understand, you mean doing if key in dict: do lookup; else: raise?

@charris
Copy link
Member

charris commented Feb 5, 2016

Well, I'd just do if key not in dict: raise :) Same difference. But if we move the dictionary out of the helper function the whole function can go away, so nothing pressing.

@madphysicist
Copy link
Contributor Author

I am not sure that using the exception is going to incurr any significant overhead. I have read in a number of places that Python exceptions are quite cheap that way, unlike say Java. However, I do have to agree that using if key not in dict: raise is much nicer to read. See here: #7199.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants