-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentinelValues.py
More file actions
75 lines (58 loc) · 1.48 KB
/
SentinelValues.py
File metadata and controls
75 lines (58 loc) · 1.48 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 11 00:46:06 2021
@author: maherme
"""
#%%
def validate(a=None):
if a is not None:
print('argument was provided')
else:
print('argument was NOT provided')
validate()
validate(10)
validate(None) # We can not differentiate this to the first call validate().
#%%
# Let's try to solve the above:
_sentinel = object()
def validate(a=_sentinel):
if a is not _sentinel:
print('argument was provided')
else:
print('argument was NOT provided')
validate()
validate(10)
validate(None)
#%%
# A better way:
def validate(a=object()):
default_a = validate.__defaults__[0]
if a is not default_a:
print('argument was provided')
else:
print('argument was NOT provided')
validate()
validate(10)
validate(None)
#%%
# If you want to use the check for more arguments:
def validate(a=object(), b=object(), *, kw=object()):
default_a = validate.__defaults__[0]
default_b = validate.__defaults__[1]
default_kw = validate.__kwdefaults__['kw']
if a is not default_a:
print('argument a was provided')
else:
print('argument a was NOT provided')
if b is not default_b:
print('argument b was provided')
else:
print('argument b was NOT provided')
if kw is not default_kw:
print('argument kw was provided')
else:
print('argument kw was NOT provided')
validate(100, 200, kw=None)
validate(200)
#%%