Python equivalent for java guava prerequisites

Does python have an equivalent java Preconditions library. eg

in java, you can check the parameters as follows:

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 a Runtime Exception, but what about python, which works best for python?

+4
source share
2 answers

While you can use assert to check conditions, the best practice in Python is usually to document the behavior of your function and then allow it to 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 

instead of testing failure cases first.

+3
source

Since I see that you passed the strings. assert and checking for empty lines will be a way. See the following code.

 >>> try: assert a is not '' except: #Do Something 
0
source

Source: https://habr.com/ru/post/1480383/


All Articles