'The Stand' by Stephen King at 1153 pages, published on January 01, 1978.Īs we can logically assume since all the arguments passed to both Book initializers were identical, our assert statement succeeded and we see the confirmation output in the log. Passing both Book instances to check_equality(a, b) produces the following output: +++++++++ BOTH INCLUDE PUBLICATION DATES +++++++++ With everything setup we can test our assertion method by creating a couple Book instances, the_stand and the_stand_2: def main(): Since a failed assert statement always raises an AssertionError, if execution continues past that statement we can assume the objects are equal and output as much to the log. args property, giving us an actual error message when catching the exception elsewhere in our code. In practical terms, this failure message argument is added to the AssertionError instance. The second argument of an assert statement is the failure message that is used as an argument if a failure occurs. The critical line is assert a = b, "The objects ARE NOT equal.", which performs an assertion that both passed objects are equivalent to one another. Most of the code here handles outputting information to the log about the equality test. The code we'll be using to test some object instances starts with the check_equality(a, b) method: def check_equality(a, b): To handle this we're using the _dict_ built-in property as a form of comparison (though we could opt for _str_(self) comparison or otherwise). Aside from that, the _eq_(self, other) method is worth noting, since this is the built-in method that will be called when attempting to check equivalence between a Book instance and another object. 'Īs usual, we perform our instance property assignment in the _init_(self, title: str = None, author: str = None, page_count: int = None, publication_date: datetime.date = None) method. """Returns a formatted string representation of Book."""ĭate = '' if self.publication_date is None else f', published on. :param publication_date: Publication Date of Book. """Determines if passed object is equivalent to current object."""ĭef _init_(self, title: str = None, author: str = None, page_count: int = None, # Assert equivalence of objects, indicating inequality if failed.Īssert a = b, "The objects ARE NOT equal." Logging.line_separator("ASSERTING EQUIVALENCE OF.") :return: Indicates if assertion was successful. """Asserts the equivalent of the two passed objects. The_hobbit_2 = Book("The Hobbit", "J.R.R. # Create two Books, one without publication_date argument specified. Logging.line_separator("ONE MISSING PUBLICATION DATE", 50, '+') # Create two Books with identical arguments. Logging.line_separator("BOTH INCLUDE PUBLICATION DATES", 50, '+') It can be copied and pasted if you'd like to play with the code yourself and see how everything works. The full exception hierarchy of this error is:īelow is the full code sample we'll be using in this article. Let's get to it! The Technical RundownĪll Python exceptions inherit from the BaseException class, or extend from an inherited class therein. We'll also dig into some functional Python code samples that illustrate how assert statements can be used, and how the failure of such a statement will raise an AssertionError that should be caught and handled, just like any other error. In this article we'll explore the AssertionError in more detail, starting with where it resides in the overall Python Exception Class Hierarchy. returns a False-y value), an AssertionError is raised. When such an assert statement fails (i.e. Like many programming languages, Python includes a built-in assert statement that allows you to create simple debug message outputs based on simple logical assertions. Making our way through our detailed Python Exception Handling series, today we're going over the AssertionError.
0 Comments
Leave a Reply. |