如何在Python中访问当前的执行模块或类名称? [英] How can I access the current executing module or class name in Python?

查看:259
本文介绍了如何在Python中访问当前的执行模块或类名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从导入的模块中动态检索当前正在执行的模块或类名称.这是一些代码:

I would like to be able to dynamically retrieve the current executing module or class name from within an imported module. Here is some code:

foo.py:

def f():
    print __name__

bar.py:

from foo import f

def b(): f()

这显然不起作用,因为__name__是包含该功能的模块的名称.我想在foo模块内部进行访问的是使用foo的当前执行模块的名称.因此,在上述情况下,它将是bar,但是如果导入了任何其他模块foo,我希望foo动态访问该模块的名称.

This obviously does not work as __name__ is the name of the module that contains the function. What I would like to be access inside the foo module is the name of the current executing module that is using foo. So in the case above it would be bar but if any other module imported foo I would like foo to dynamically have access to the name of that module.

inspect模块看起来非常有前途,但它并不是我想要的.我希望找到的是我可以访问的某种全局或环境级别的变量,其中包含当前执行模块的名称.并不是说我不愿意遍历堆栈以找到该信息-我只是以为Python可能已经公开了该数据.

The inspect module looks quite promising but it is not exactly what I was looking for. What I was hoping for was some sort of global or environment-level variable that I could access that would contain the name of the current executing module. Not that I am unwilling to traverse the stack to find that information - I just thought that Python may have exposed that data already.

编辑:这是我尝试使用的方法.我有两个不同的Django应用程序,都需要将错误记录到文件中.可以说它们分别称为"AppOne"和"AppTwo".我还有一个要记录这些文件的位置:"/home/hare/app_logs".在每个给定点的每个应用程序中,我都希望能够导入记录器模块并调用log函数,该函数将日志字符串写入文件.但是,我想做的是在app_logs下创建一个目录,该目录是当前应用程序的名称("AppOne"或"AppTwo"),以便每个应用程序的日志文件都将进入各自的日志目录中.

Here is how I am trying to use this. I have two different Django applications that both need to log errors to file. Lets say that they are called "AppOne" and "AppTwo". I also have a place to which I would like to log these files: "/home/hare/app_logs". In each application at any given point I would like to be able to import my logger module and call the log function which writes the log string to file. However what I would like to do is create a directory under app_logs that is the name of the current application ("AppOne" or "AppTwo") so that each application's log files will go in their respective logging directories.

为此,我认为最好的方法是使logger模块能够访问某种表示全局变量的全局变量,该变量表示当前应用程序的名称,因为它负责了解父级日志记录目录的位置并创建应用程序的日志目录(如果尚不存在).

In order to do this I thought that the best way would be for the logger module to have access to some sort of global variable that denotes the current application's name as it is responsible for knowing the location of the parent logging directory and creating the application's logging directory if it does not yet exist.

推荐答案

从评论开始,而不是问题.

From the comment -- not the question.

我只是想知道我尝试做的事是否可能.

I am simply curious to see if what I am trying to do is possible.

是否可能"的答案始终是是".总是.除非您的问题涉及时间旅行,反重力或恒动.

The answer to "is it possible" is always "yes". Always. Unless your question involves time travel, anti-gravity or perpetual motion.

由于答案始终是是",因此您的问题形式不正确.真正的问题是让我的日志记录模块知道客户端名称的好方法是什么?"或类似的东西.

Since the answer is always "yes", your question is ill-formed. The real question is "what's a good way to have my logging module know the name of the client?" or something like that.

答案是接受它作为参数".不要胡乱检查或寻找神秘的全局元素或其他技巧.

The answer is "Accept it as a parameter." Don't mess around with inspecting or looking for mysterious globals or other tricks.

只需遵循logging.getLogger()的设计模式并使用显式命名的记录器即可.常见的用法如下

Just follow the design pattern of logging.getLogger() and use explicitly-named loggers. A common idiom is the following

logger= logging.getLogger( __name__ )

几乎可以完美处理所有日志命名.

That handles almost all log naming perfectly.

这篇关于如何在Python中访问当前的执行模块或类名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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