How to get a variable data type in Python 3 All In One

发布时间 2023-09-19 20:27:58作者: xgqfrms

How to get a variable data type in Python 3 All In One

typeof in js

type(var) & isinstance(var, type)

#!/usr/bin/env python3

# mix list
lt = [1, 2, {'k': 'v'}, {1, 'str'}]

dt = dict()
for [i, item] in enumerate(lt):
  print(i, item)
  dt[i] = item
print('\n', dt)


st = {'str', 3}
print('\n', st)
print("type(st) =", type(st))
# type(st) = <class 'set'>
print(isinstance(st, set))
# True

map = {'key1': 1, 'key2': 1}
print('\n', map)
print("type(map) =", type(map))
# type(map) = <class 'dict'>
print(isinstance(map, dict))
# True


""" 

$ py3 ./py-set.py
$ python3 ./py-set.py

"""

image

python env

$ ls -alth  /usr/bin/ | grep env
-rwxr-xr-x    1 root   wheel   132K Jun 15 18:08 env
-rwxr-xr-x    1 root   wheel   131K Jun 15 18:08 printenv


$ which python
python: aliased to /usr/local/bin/python3
$ which python3
/usr/bin/python3
$ which py3
py3: aliased to python

shebang env

#!

#!/usr/bin/env python3

#!/usr/bin/python3

demos

(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!