File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed
source-code/error-handling Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -8,3 +8,5 @@ Illustrations of error handling.
8
8
ways ot handle errors.
9
9
1 . ` data.txt ` : data file to use as input.
10
10
1 . ` data_not_ok.txt ` : incorrect data file to use as input.
11
+ 1 . ` warnings ` : illustrations of using the ` warmings ` module
12
+ for error handling.
Original file line number Diff line number Diff line change
1
+ # Warnings
2
+
3
+ Python supports warnings and errors at runtime. This can provide
4
+ a useful error handling mechanism for your code. You can use the
5
+ ` warnings ` module to issue warnings and errors.
6
+
7
+
8
+ ## What is it?
9
+
10
+ 1 . ` will_warn.py ` : script that issues a DeprecationWarning, intended
11
+ to illustrate how to suppress it at runtime.
12
+
13
+
14
+ ## How to use it?
15
+
16
+ You can suppress the warning from the command line in two ways:
17
+ * using the ` -W ` option to the Python interpreter, and
18
+ * setting the ` PYTHONWARNINGS ` environment variable.
19
+
20
+ ``` bash
21
+ $ python -Wi::DeprecationWarning will_warn.py
22
+ ```
23
+
24
+ ``` bash
25
+ $ export PYTHONWARNINGS=" ignore::DeprecationWarning"
26
+ $ python will_warn.py
27
+ ```
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+
3
+ import warnings
4
+
5
+
6
+ class MyClass :
7
+
8
+ def __init__ (self , value ):
9
+ self ._value = value
10
+
11
+ @property
12
+ def value (self ):
13
+ return self ._value
14
+
15
+ def get_value (self ):
16
+ warnings .warn ("get_value is deprecated. Use the value property" ,
17
+ DeprecationWarning )
18
+ return self ._value
19
+
20
+
21
+ if __name__ == '__main__' :
22
+ obj = MyClass (42 )
23
+ print (obj .get_value ())
24
+ print (obj .value )
You can’t perform that action at this time.
0 commit comments