June 2019
Beginner to intermediate
770 pages
19h 24m
English
When we change the API for one of our modules, packages, or classes, we can provide a handy marker via the warnings module. This will raise a warning in the method that is deprecated or is pending deprecation, as follows:
import warningsclass Player: """version 2.1""" def bet(self) -> None: warnings.warn( "bet is deprecated, use place_bet", DeprecationWarning, stacklevel=2) pass
When we do this, any part of the application that uses Player.bet() will receive DeprecationWarning. By default, this warning is silent. We can, however, adjust the warnings filter to see the message, as shown here:
>>> warnings.simplefilter("always", category=DeprecationWarning) >>> p2 = Player() >>> p2.bet() __main__:4: DeprecationWarning: ...Read now
Unlock full access