正确的python变量名_在Python,如何将变量名作为字符串?_others_酷徒编程知识库...
我編寫了一個小函數來對代碼中的各種變量進行快速內聯頭檢查,它列出了變量名,數據類型,大小和其他屬性,所以,我可以快速捕捉到我所犯的任何錯誤,代碼很簡單:def details(val):
vn = val.__name__ # If such a thing existed
vs = str(val)
print("The Value of "+ str(vn) + " is " + vs)
print("The data type of " + vn + " is " + str(type(val)))
因此,如果你有一些復雜的dictionary/list/tuple情況,那么讓解釋器返回你指定的變量名是非常有幫助,例如下面是一個奇怪的字典:m = 'abracadabra'
mm=[]
for n in m:
mm.append(n)
mydic = {'first':(0,1,2,3,4,5,6),'second':mm,'third':np.arange(0.,10)}
details(mydic)
The Value of mydic is {'second': ['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a'], 'third': array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]), 'first': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}
The data type of mydic is
details(mydic['first'])
The Value of mydic['first'] is (0, 1, 2, 3, 4, 5, 6)]
The data type of mydic['first'] is
details(mydic.keys())
The Value of mydic.keys() is ['second', 'third', 'first']
The data type of mydic.keys() is
details(mydic['second'][0])
The Value of mydic['second'][0] is a
The data type of mydic['second'][0] is
總結
以上是生活随笔為你收集整理的正确的python变量名_在Python,如何将变量名作为字符串?_others_酷徒编程知识库...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vs矩形框边框线显示被选中的区域;_条形
- 下一篇: 生成器 python0 1 8 27 6