问题标签 [inspect]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
55578 浏览

python - 获取完整的包模块名称

对于我的应用程序中的详细调试消息,我使用了一个返回有用前缀的函数。考虑以下示例:

这输出:


我的问题是:当我在包中有一个模块时,例如“myproject.utilities.input”,返回的模块名称get_verbose_prefix仍然只是“input”,而不是“myproject.utilities.input”。当不同的子模块中可能有多个“输入”模块一起工作时,这大大降低了前缀在大型项目中的有用性。

所以我的问题是:有没有一种简单的方法可以在 Python 的包中检索完整的模块名称?我计划扩展该get_verbose_prefix功能以检查模块父目录中的“__init__.py”文件以推断它的全名,但首先我想知道是否有更简单的方法来做到这一点。

0 投票
2 回答
3987 浏览

python - Python - How can I query the class in which a method is defined?

My question is somewhat similar to this one; it concerns object methods rather than module contents. I want to know if I can use the inspect module to get the methods defined only in the class I'm asking about, and not its parent(s).

I need this because my child class defines 'macro' methods, which accesses the parent's methods at a higher level of abstraction, and I don't want the user to have to worry about the lower-level methods defined all the way up the inheritance tree.

Here is a simplified example:

Output:

The user need not know or care about the existence of the fs since she's only ever going to be interested in the gs. (Of course, this output makes sense in the vast majority of contexts, since all these methods will be bound to the object when it is instantiated.) For a long inheritance tree, the returned list can get very long and full of things that aren't relevant to the user.

How can I get it to leave f1 and f2 off this list? Is there an equivalent to the __module__ attribute for the methods defined in classes? Better still, is it possible to do the same thing with instance methods?

0 投票
2 回答
291 浏览

python - os.chdir 之后inspect.getmodule 的结果变化

我的一些代码似乎突然出现了一个错误。当我追踪它时,我发现inspect.getmodule在执行之前和之后使用相同(相同)的参数进行评估os.chdir会产生不同的结果。

(FWIW,我在 OS X 下运行 Python 2.7.3。)

我发现这种行为非常令人费解和困惑,以至于我不知道如何最好地修复这个错误。(换句话说,在了解行为的原因后,我能想到的任何解决方案都只是一个创可贴。)

下面的测试脚本说明了这种行为(当然,现实生活中的错误发生在更加现实和复杂的环境中):

(注意:运行此代码需要在当前目录下立即有一个名为“subdir”的子目录。)

它的输出是:

请注意,尽管所有三个调用getmodule都具有相同的框架对象作为参数,但最后一个的结果与前两个的结果不同。(正如人们所期望的那样,前两次调用的结果是相同的。)

前两个调用和第三个调用的唯一区别是后者发生在运行之后os.chdir

任何有关正在发生的事情的见解将不胜感激。

0 投票
5 回答
4514 浏览

python - 在python上打印出类参数

我想打印出类 Column 的实例化参数。通常 - python 代码到字符串。例子:

我尝试了检查模块,但发现它不支持它: http ://docs.python.org/library/inspect.html#inspect.getmembers

感谢您提供任何信息

0 投票
4 回答
214 浏览

javascript - 从第三方代码中暴露未知的 JavaScript 对象方法

我正在寻找一种方法来公开未知 JavaScript 对象的类型和方法。

我正在实现第三方代码,其方法的文档有限,因此我无权访问任何可以告诉我应该在response.

有没有办法确定这是什么类型的对象并公开它包含的方法?

Firebug 控制台中的输出:

响应:[对象对象],[对象对象],[对象对象],....

0 投票
3 回答
125 浏览

python - 如何在运行时从模块内部知道哪个脚本调用了其中的函数

我在 python 中有一个模块,并且基于在其中调用函数的脚本,我想在该模块中做出决定。

因此,如果我们有 2 个文件file1.pyfile2.py,则都导入模块 testmod 并在其中调用一个函数。在模块 testmod 中,我想知道哪个脚本调用了它?file1.pyfile2.py

我想在 testmod 中编写如下代码 if then do this else if then do that else do something else !

0 投票
1 回答
4266 浏览

python - 使用检查.getmembers

我正在尝试使用inspect.getmembers来检查文件中的类和函数。问题是我不知道如何在inspect.getmembers不使用import. 那是因为我每次都需要指定不同的文件名

代码与此类似:

0 投票
1 回答
144 浏览

assembly - 检查组装中的堆栈

我正在内联汇编中推送一些 int 值:

后来我检索这个值做弹出:

我想在不弹出的情况下检查我的堆栈。我需要重新排列或重新审视一些值。完成后我可以恢复堆栈。

我对asm很生疏。有没有类似的东西:

那会移动下一个(不是当前的)堆栈元素吗?我只是猜测。我需要这段代码在 32 位 win 和 64 win 环境中运行。

0 投票
1 回答
763 浏览

python - 计算python文件中每个类的代码行数?

我正在尝试在 python 文件中查找类中的代码行数。我使用 len(inspect.getsourcelines(b)[0]) 返回文件中每个类的代码行数但是除了代码行之外,它还计算注释和文档字符串的问题..有没有办法丢弃注释和文档字符串?

0 投票
1 回答
39 浏览

ajax - 当 Google Maps 网站显示可自动完成的值时,不会执行 HTTP 请求。这怎么可能?

我正在使用FirefoxFirebug来了解和了解 AJAX 的工作原理。我正在“检查”谷歌地图网站,当我移动到输入一些名称的搜索输入字段时,它会显示一个可自动完成的值列表。然而,奇怪的是,为了检索这些值,执行了任何 AJAX HTTP 请求。这怎么可能?!也就是说,谷歌地图是如何检索这些值的?

注意:在Google网站上不会发生同样的情况。