forked from WilliamQLiu/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.py
More file actions
33 lines (23 loc) · 844 Bytes
/
assert.py
File metadata and controls
33 lines (23 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
https://wiki.python.org/moin/UsingAssertionsEffectively
Asserts are good for debugging, helps do internal self-checks
"""
from types import * # for asserting types
class Person:
''' Simple class to show assertions '''
def __init__(self, age, color):
self._myage = age
self._mycolor = color
def check_types(self):
assert type(self._myage) is IntType, "Error - myage is not an integer, instead it is %r" % self._myage
assert type(self._mycolor) is StringType, "Error - mycolor is not a string, instead it is %r" % self._mycolor
if __name__ == '__main__':
# Will Pass
Will = Person(10, "Hello")
Will.check_types()
# Will Fail on int
Laura = Person("20", "Hi")
Laura.check_types()
# Will Fail on string
Billy = Person(30, 40)
Billy.check_types()