本文介绍了任何人都有一个很好的“view_var”程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好, 是否有一些方便/好的方式来查看某个变量的属性? 作为一个新手,我经常想看到想要的var的所有属性, 以及一些角点值(大数组)等。 可能它不是那么困难, 但我不知道如何区分字符串和 数组。数组有一个形状,一个字符串不等。 谢谢, Stef Mientki 解决方案 在星期一15/1/2007 17:45,Stef Mientki写道: >是否有一些方便/好的方式来查看某些变量的属性? 作为一个新手,我经常想要看到var的所有属性,以及一些角落值(大型数组)等等。 您可以尝试dir(x),vars(x)。如果你想要一个漂亮的打印,请参阅pprint模块。 pyimport csv pyx = csv.DictReader('''')#a假对象 pyx < csv.DictReader实例位于0x00BC79B8> pydir(x) [' '__doc__'',''__ init__'',''__ ____',''_ _ _ _ _ _ _'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /> ,''restkey'',''restval''] pyvars(x) {''restkey'':无,'' restval'':无,''fieldnames'':无,''读者'': < _csv.reader ob ject at 0x00BC98B8>} pyfrom pprint import pprint pypprint(vars(x)) {''fieldnames'':无, ' 'reader'':< _csv.reader对象位于0x00BC98B8>, ''restkey'':无, ''restval'':无} py> >可能它不是那么困难,但我不知道如何区分字符串和数组。数组有一个形状,一个字符串不是等等。 是的,字符串与其他序列类型共享许多功能: [] ,len,in等。您可以使用 isinstance(obj,str)测试对象是否为字符串。如果你想包括unicode对象,请使用 isinstance(obj,basestring)。 - Gabriel Genellina Softlab SRL __________________________________________________ $ b $bPreguntá。 Respondé。 Descubrí。 Todo loquequeríasaber,y lo que ni imaginabas, $ b $bestáenYahoo! Respuestas(测试版)。 ?Probalo ya! http://www.yahoo.com.ar/respuestas Stef Mientki写道: 你好, 是否有一些方便/好的方式来查看某些变量的属性? 作为一个新手,我经常想要看到var的所有属性, 以及一些角落值(大型数组)等。 可能它不是那么困难, 但是我没看到如何区分字符串和 数组之间。数组有一个形状,一个字符串不是等。 谢谢, Stef Mientki 我也是新手,所以如果这不是你想要的,我就不能给予更多 更多。 你可以使用类型()检查变量是什么。用法: #------------- Python代码------------------------ ----- >> aVar = [" zero",1 ,float(2)] type(aVar) < type''list''> >> type(aVar [0]) < type's'tr''> >> type(aVar [1])$ ​​b $ b < type''int''> >> type(aVar [2]) < type''flora''> >> if type(aVar [1])== int: print" test" test # - ----------------结束代码------------------------------- 还有像len()这样的函数。用法: #------------- Python代码------------------------ ----- >> len(aVar) 3 >> len(aVar [0]) 4 #--- --------------结束代码------------------------------- 希望这会有所帮助。 Adam Stef Mientki写道: 你好, 是否有一些方便/好的方式来查看某个变量的属性? 新手,我经常想要看到var的所有属性, 以及一些角落值(大型阵列)等。 可能它''没那么难, 但是我我们没有看到如何区分字符串和 数组。数组有一个形状,一个字符串不是等。 谢谢, Stef Mientki 其他人已经提出了一些建议,但我认为我也会提出我的 想法。当我第一次开始使用Python时,我正在寻找 。使用一段时间之后,你开始理解了Python中的对象可能如此复杂(字典列表中包含列表和其他字典的列表,类实例列表,可以 包含其他类实例,可以包含....,你得到图片。) 你必须开始考虑使用dir导航这个( ), vars()和pprint.pprint()。与旧的编程语言不同, 对于如何在对象中混合/匹配值没有限制。 另外在标准Python列表中(数组?)don没有形状(在模块上添加了 ,为您提供此类功能)。一切都是 的对象集合。 class foo: def __init __(self): self.x = 1 self.y = 2 l = [[1,2,3],''test'',6, 3.14159,{'''':1,''b'':2},foo()] a list with: a list as first元素 a字符串作为第二个元素 整数作为第三个元素 a浮动作为第三个元素 a字典作为第四个元素 a类实例作为第五个元素 如果你还没有看过这个教程是一个很好的起点 it。 -Larry Bates hello,Is there some handy/ nice manner to view the properties of some variable ?As a newbie, I often want to see want all the properties of a var,and also some corner values (large arrays) etc.Probably it''s not so difficult,but I don''t see how to distinguish for example between a string and anarray. An array has a shape, a string not etc.thanks,Stef Mientki 解决方案 At Monday 15/1/2007 17:45, Stef Mientki wrote:>Is there some handy/ nice manner to view the properties of some variable ?As a newbie, I often want to see want all the properties of a var,and also some corner values (large arrays) etc.You can try dir(x), vars(x). If you want a "nice print", see the pprint module.pyimport csvpyx = csv.DictReader('''') # a bogus objectpyx<csv.DictReader instance at 0x00BC79B8>pydir(x)[''__doc__'', ''__init__'', ''__iter__'', ''__module__'', ''fieldnames'',''next'', ''reader'', ''restkey'', ''restval'']pyvars(x){''restkey'': None, ''restval'': None, ''fieldnames'': None, ''reader'':<_csv.reader object at 0x00BC98B8>}pyfrom pprint import pprintpypprint(vars(x)){''fieldnames'': None,''reader'': <_csv.reader object at 0x00BC98B8>,''restkey'': None,''restval'': None}py>>Probably it''s not so difficult,but I don''t see how to distinguish for example between a string and anarray. An array has a shape, a string not etc.Yes, strings share a lot of functionality with other sequence types:[], len, in, etc. You can test if an object is a string usingisinstance(obj, str). If you want to include unicode objects too, useisinstance(obj, basestring).--Gabriel GenellinaSoftlab SRL__________________________________________________Preguntá. Respondé. Descubrí.Todo lo que querías saber, y lo que ni imaginabas,está en Yahoo! Respuestas (Beta).?Probalo ya! http://www.yahoo.com.ar/respuestasStef Mientki wrote:hello,Is there some handy/ nice manner to view the properties of some variable ?As a newbie, I often want to see want all the properties of a var,and also some corner values (large arrays) etc.Probably it''s not so difficult,but I don''t see how to distinguish for example between a string and anarray. An array has a shape, a string not etc.thanks,Stef MientkiI am also a newbie so if this is not what you want I can''t give muchmore as of yet.You can use type() to check what a Variable is. Usage:#-------------Python Code----------------------------->>aVar = ["zero", 1, float(2)]type(aVar)<type ''list''>>>type(aVar[0])<type ''str''>>>type(aVar[1])<type ''int''>>>type(aVar[2])<type ''float''>>>if type(aVar[1]) == int:print "test"test#-----------------End Code-------------------------------There are also function like len(). Usage:#-------------Python Code----------------------------->>len(aVar)3>>len(aVar[0])4#-----------------End Code-------------------------------Hope this helps.Adam Stef Mientki wrote:hello,Is there some handy/ nice manner to view the properties of some variable ?As a newbie, I often want to see want all the properties of a var,and also some corner values (large arrays) etc.Probably it''s not so difficult,but I don''t see how to distinguish for example between a string and anarray. An array has a shape, a string not etc.thanks,Stef MientkiOthers have given some suggestions, but I thought I''d put in mythoughts also. When I first started using Python I was looking forexactly what you describe. After using a while, you begin to understandthat the objects in Python can be so complex (lists of dictionaries thatcontain lists and other dictionaries, lists of class instances that cancontain other class instances, that can contain...., you get the picture).You have to begin to think about navigating through this by using dir(),vars() and pprint.pprint(). Unlike older programming languages there isno limit to how you can mix/match values within an object.Also in standard Python lists (arrays?) don''t have shape (there are addon modules that give you this type of feature). Everything is acollection of objects.class foo:def __init__(self):self.x=1self.y=2l=[[1,2,3], ''test'', 6, 3.14159, {''a'':1, ''b'':2}, foo()]a list with:a list as first elementa string as the second elementan integer as the third elementa float as the third elementa dictionary as the fourth elementa class instance as the fifth elementThe tutorial is a good place to start if you haven''t already looked atit.-Larry Bates 这篇关于任何人都有一个很好的“view_var”程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 04:11