欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

正确的python变量名_在Python,如何将变量名作为字符串?_others_酷徒编程知识库...

发布时间:2025/3/15 python 67 豆豆
生活随笔 收集整理的这篇文章主要介绍了 正确的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_酷徒编程知识库...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。