在python中,是否有一种方法可以从对象本身中找到包含变量或其他对象的模块?

问题描述:

例如,假设我定义了一个变量,其中可能有多个

As an example, say I have a variable defined where there may be multiple

from __ import *
from ____ import *

有没有办法弄清楚命名空间中变量之一的定义位置?

Is there a way to figure out where one of the variables in the namespace is defined?

编辑

谢谢,但是我已经知道import *通常被认为是不良形式.但这不是问题,无论如何我都没有写.有一种方法可以找到变量的来源,这真是太好了.

Thanks, but I already understand that import * is often considered poor form. That wasn't the question though, and in any case I didn't write it. It'd just be nice to have a way to find where the variable came from.

这就是为什么在大多数情况下在python中使用from __ import *被认为是不好的形式的原因.使用from __ import myFuncimport __ as myLib.然后,当您需要myLib中的某些内容时,便不会重叠.

This is why it is considered bad form to use from __ import * in python in most cases. Either use from __ import myFunc or else import __ as myLib. Then when you need something from myLib it doesn't over lap something else.

有关在当前名称空间中查找内容的帮助,请查看 pprint库内置目录以及内置的全局变量.

For help finding things in the current namespace, check out the pprint library, the dir builtin, the locals builtin, and the globals builtin.