在python中,是否有一种方法可以从对象本身中找到包含变量或其他对象的模块? [英] in python, is there a way to find the module that contains a variable or other object from the object itself?

查看:86
本文介绍了在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.

这篇关于在python中,是否有一种方法可以从对象本身中找到包含变量或其他对象的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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