Does python have any equivalent to java Preconditions library. e.g.
in java, you can check the parameters like this:
public void dummymethod(Cat a, Dog b) {
Preconditions.checkNotNull(a, "a can not be null.");
Preconditions.checkNotNull(b, "b can not be null.");
/**
your logic
**/
}
If a or b is null, Java throws Runtime Exception, How about python, what's the best practice here for python?
While you can use assert
to verify conditions, the best practice in Python is normally to document the behaviour of your function, and then let it fail if the preconditions are not met.
For example, if you were reading from a file, you would do something like:
def read_from_file(filename):
f = open(filename, 'rU') # allow IOError if file doesn't exist / invalid permissions
rather than testing for failure cases first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With