Python-100-Days/公开课/文档/年薪50W+的Python程序员如何写代码/code/Python/opencourse/part02/idiom06.py

15 lines
302 B
Python

data = {'x': '5'}
# if 'x' in data and isinstance(data['x'], (str, int, float)) \
# and data['x'].isdigit():
# value = int(data['x'])
# print(value)
# else:
# value = None
try:
value = int(data['x'])
print(value)
except (KeyError, TypeError, ValueError):
value = None