枚举或列出 [您最喜欢的语言] 程序中的所有变量 [英] Enumerate or list all variables in a program of [your favorite language here]

查看:11
本文介绍了枚举或列出 [您最喜欢的语言] 程序中的所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周一个朋友问我如何枚举或列出程序/函数/等中的所有变量.出于调试的目的(基本上是获取所有内容的快照,以便您可以查看设置了哪些变量,或者是否设置了变量).我环顾了一下,找到了一个比较好的Python方法:

<前>#!/usr/bin/pythonfoo1 = "你好世界"foo2 = "酒吧"foo3 = {"1":"a","2":"b"}foo4 = "1+1"对于 dir() 中的名称:我的价值 = 评估(名称)打印名称, "is", type(name), "and is equal to ", myvalue

这将输出如下内容:

<前>__builtins__ 是 <type 'str'>并且等于 <module '__builtin__' (built-in)>__doc__ 是 <type 'str'>并且等于 None__file__ 是 <type 'str'>并且等于 ./foo.py__name__ 是 <type 'str'>并且等于 __main__foo1 是 并且等于 Hello worldfoo2 是 并且等于 barfoo3 是 并且等于 {'1': 'a', '2': 'b'}foo4 是 并且等于 1+1

到目前为止,我已经在 PHP 中找到了部分方法(由 链接文本提供) 但它只列出所有变量及其类型,而不是内容:

<前><?php//创建几个变量$bar = 'foo';$foo ='bar';//创建一个新的数组对象$arrayObj = new ArrayObject(get_defined_vars());//遍历数组对象并回显变量和值for($iterator = $arrayObj->getIterator(); $iterator->valid(); $iterator->next()){echo $iterator->key() .' =>' .$iterator->current() .'<br/>';}?>

所以我告诉你:你如何用你最喜欢的语言列出所有变量及其内容?

<小时>

Edit by VonC:我建议这个问题遵循一点代码挑战".
如果您不同意,只需编辑并删除标签和链接即可.

解决方案

在 python 中,使用 locals 返回一个包含所有本地绑定的字典,从而避免 eval:

<预><代码>>>>foo1 = "你好世界">>>foo2 = "酒吧">>>foo3 = {"1":"a",... "2":"b"}>>>foo4 = "1+1">>>导入打印>>>pprint.pprint(locals()){'__builtins__': <module '__builtin__'(内置)>,'__doc__':无,'__name__': '__main__','foo1': '你好世界','foo2': '酒吧','foo3': {'1': 'a', '2': 'b'},'foo4': '1+1','pprint':<来自'/usr/lib/python2.5/pprint.pyc'的模块'pprint'>}

A friend asked me last week how to enumerate or list all variables within a program/function/etc. for the purposes of debugging (essentially getting a snapshot of everything so you can see what variables are set to, or if they are set at all). I looked around a bit and found a relatively good way for Python:

#!/usr/bin/python                                                                                                                                                                                                                           
foo1 = "Hello world"
foo2 = "bar"
foo3 = {"1":"a",
        "2":"b"}
foo4 = "1+1"

for name in dir():
    myvalue = eval(name)
    print name, "is", type(name), "and is equal to ", myvalue

which will output something like:

__builtins__ is <type 'str'> and is equal to  <module '__builtin__' (built-in)>
__doc__ is <type 'str'> and is equal to  None
__file__ is <type 'str'> and is equal to  ./foo.py
__name__ is <type 'str'> and is equal to  __main__
foo1 is <type 'str'> and is equal to  Hello world
foo2 is <type 'str'> and is equal to  bar
foo3 is <type 'str'> and is equal to  {'1': 'a', '2': 'b'}
foo4 is <type 'str'> and is equal to  1+1

I have so far found a partial way in PHP (courtesy of link text) but it only lists all variables and their types, not the contents:

<?php
// create a few variables
$bar = 'foo';
$foo ='bar';
// create a new array object
$arrayObj = new ArrayObject(get_defined_vars());
// loop over the array object and echo variables and values
for($iterator = $arrayObj->getIterator(); $iterator->valid(); $iterator->next())
        {
        echo $iterator->key() . ' => ' . $iterator->current() . '<br />';
        }
?>

So I put it to you: how do you list all variables and their contents in your favorite language?


Edit by VonC: I propose this question follows the spirit of a little "code-challenge".
If you do not agree, just edit and remove the tag and the link.

解决方案

In python, using locals which returns a dictionary containing all the local bindings, thus, avoiding eval:

>>> foo1 = "Hello world"
>>> foo2 = "bar"
>>> foo3 = {"1":"a",
...         "2":"b"}
>>> foo4 = "1+1"

>>> import pprint
>>> pprint.pprint(locals())
{'__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__name__': '__main__',
 'foo1': 'Hello world',
 'foo2': 'bar',
 'foo3': {'1': 'a', '2': 'b'},
 'foo4': '1+1',
 'pprint': <module 'pprint' from '/usr/lib/python2.5/pprint.pyc'>}

这篇关于枚举或列出 [您最喜欢的语言] 程序中的所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆