构造 - 模块属性和执行环境 [英] CONSTRUCT - Module Attributes and Execution Environment

查看:51
本文介绍了构造 - 模块属性和执行环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改构造:


if __name__ ==''__ main__'':


类似于:


如果exec.isMain():


My(OO思想)是将一个类放在一个单独的代码模块中并且

实例化一个单例实例,它会保留像

这样的东西:


class ExecutionEnv:

def isMain (个体经营)

如果CALLING_MODULE .__ name__ ==''__ main__'':

返回true

其他

返回false


exec = ExecutionEnv()

如何访问CALLING_MODULE?


-


是否可以使用替代构造/机制,这可能是用于将此功能直接添加到代码模块的


I would like to change the construct:

if __name__ == ''__main__'':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == ''__main__'':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?

推荐答案



lazaridis_com写道:

lazaridis_com wrote:

I想改变构造:


if __name__ ==''__ main__'':


类似于:


如果exec.isMain():


My(OO思想)是将一个类放在一个单独的代码模块中并且

实例化一个单例这个实例会保留像

这样:


class ExecutionEnv:

def isMain(self)

如果CALLING_MODULE .__ name__ ==''__ main__'':

返回true

else

返回false


exec = ExecutionEnv()

如何访问CALLING_MODULE?
I would like to change the construct:

if __name__ == ''__main__'':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == ''__main__'':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?



sys._getframe(1).f_globals [''__ name__'']


一切顺利,

Fuzzyman
http: //www.voidspace.org.uk/python/index.shtml


>

-


是否可以使用替代构造/机制,这可能是用于将此功能直接添加到代码模块的

>
-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?


lazaridis_com写道:
lazaridis_com wrote:

是否有其他可用的构造/机制,可能是

用于将此功能直接添加到代码模块吗?
Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?



这些内容如何:


--------------- --- auto.py ---------

import sys,atexit

def main_body(f):

如果f.func_globals [''__ name __''] ==''__ main__'':

atexit.register(f,sys.argv)

返回f


@main_body

def auto(args):

print" auto run",args

------------------------------------


如果您将auto.py作为脚本运行,则执行已修饰的函数。如果你导入它b / b $ b $那么装饰函数就不会执行。在你自己的剧本中

你只需要一个import语句并将装饰器放在你的主

函数上。

How about something along these lines:

------------------ auto.py ---------
import sys, atexit

def main_body(f):
if f.func_globals[''__name__'']==''__main__'':
atexit.register(f, sys.argv)
return f

@main_body
def auto(args):
print "auto run", args
------------------------------------

If you run auto.py as a script then the decorated function executes. If you
import it then the decorated function doesn''t execute. In your own script
you just need an import statement and to put the decorator on your main
function.


lazaridis_com写道:
lazaridis_com wrote:

我想更改构造:


if __name__ ==''__ main__'':


类似于:


如果exec.isMain():


我的(OO思考)是将一个类放在一个单独的代码模块中,并且

实例化一个单例实例,它将保留像

这样的东西:


class ExecutionEnv:

def isMain(self)

如果CALLING_MODULE .__ name__ ==''__ main__'':

返回true

其他

返回false

exec = ExecutionEnv()


如何我可以访问CALLING_MODULE吗?


-


是否可以使用替代构造/机制,w hich可能是

用于将此功能直接添加到代码模块中吗?
I would like to change the construct:

if __name__ == ''__main__'':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == ''__main__'':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?



两个想法:

1)不要调用类实例exec,它会掩盖内置的

exec语句。


2)恕我直言如果__name__ ==" __ main __"所有建议都比

更复杂。并且对于大多数pythoneers来说不是SOP。

我知道前一个构造意味着/做了什么。我必须打破你的课程来解释你的课程,以便我们知道后者的作用和它

并不能真正为你节省任何代码或提供表现

增强。


-Larry

Two thoughts:

1) Don''t call a class instance exec, it will mask the built-in
exec statement.

2) IMHO all the suggestions are way more complicated than
if __name__ == "__main__" and are not SOP for most pythoneers.
I know what the former construct means/does. I have to
decipher your class to figure our what the latter does and it
doesn''t really save you any code or provide a performance
enhancement.

-Larry


这篇关于构造 - 模块属性和执行环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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