一个创业难题 [英] A startup puzzle

查看:49
本文介绍了一个创业难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚确信自己没有好的,干净的解决方案来跟随这个难题。我想知道你是否同意。


1.我的应用程序有一个名为leoGlobals的模块,其中包含经常使用的全局

函数。 Leo的所有源文件都以:

来自leoGlobals导入的
*


我不想讨论这是否是好的风格:它很简单,它对我来说很有效:-)


2.所有代码都使用app()访问单例应用程序对象在leoGlobals中使用
方法。


3.为了使代码更清晰,我真的希望代码是

能够访问应用程序全局而不是app()函数。这与

无关:它只是在代码中有很多引用

app()的内容,很快就会有更多参考了

app.gui.x()和app.gui.y()等。


作为一种解决方法,有很多方法和功能指定一个= app(),但是我会想要避免这一步。


4.唉,似乎无法初始化全局应用在

leoGlobals。原因很简单:来自leoGlobals import的
*


在导入完成时缓存app的值。但创业应用程序全局的创业公司

代码(真的是leoGlobals

模块的一个属性)必须做_other_进口。


例如,在leoGlobals顶部执行以下操作是行不通的:


导入leoApp

app = leoApp.leoApp()#构建app实例。


的确, leoApp模块将在分配应用程序之前导入。

此外,leoApp模块执行其他导入,并且这些模块中的所有应用程序变量都将是未初始化的。




中的订单进行详细限制将是一种可怕的风格,哪些模块会被导入,我甚至不确定这是否会起作用。


任何干净解决方案的想法?谢谢。


爱德华


P.S.解决方案必须在2.1,2.2和2.3中工作。


EKR

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

Edward K. Ream电子邮件: ed ******* @ charter.net

Leo:带有轮廓的文学编辑器

Leo: http://webpages.charter.net/edreamleo/front.html

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

解决方案

Edward K. Ream写道:

我只是说服自己在那里
下面的谜题是不好的,干净的解决方案。我想知道你是否同意。




如果我理解正确的话,你会这样做


leoGlobals.py

_app =无

def app():

global _app

如果不是_app:

#其他代码

_app =申请()

返回_app


leoMain.py
来自leoGlobals的
导入*

app = app()

app.mainLoop()


leoOther.py

来自leoGlobals导入*


app()。doSomething()

您的程序是否会在没有创建应用程序的情况下运行

实例?如果没有,懒惰的创造就没有意义了:


leoGlobals.py

app =无

def createApp( ):

全球应用

断言不是app

app =应用程序()


leoMain。来自leoGlobals进口的
*

createApp()

app.mainLoop()

leoOther 。le
来自leoGlobals导入的
*

#如果您可以确保leoMain.py是

#only入口点,则此方法有效到您的应用程序

app.doSomething()

如果我遗漏了某些内容,如果你发布类似的代码

片段可能会有所帮助上述计划将无效。


彼得



2003年9月29日星期一11:31:05 -0500,Edward K. Ream

< ed ******* @ charter.net>写道:

任何干净解决方案的想法?谢谢。




我不确定我明白你在说什么,但我会提出一个

的快速建议......


也许''app''可以是''leoGlobals''中的全局变量,但

在模块中分配值''None''是进口的。然后你的
初始化代码可以在相应的

时间内重新分配变量。


如果你参考''app.whatever ''虽然''app''仍然是''没有'你应该

得到一个例外。


这不是大多数人的意思当然,通过''singleton',但是如果你

可以确定你的初始化顺序,它可以做

的工作。

-

Steve Horne


steve at ninereeds dot fsnet dot co uk


Stephen Horne <

I''ve just about convinced myself there is no good, clean solution to the
following puzzle. I wonder if you agree.

1. My app has a module called leoGlobals that contains often-used global
functions. All of Leo''s source files start with:

from leoGlobals import *

I don''t want to discuss whether this is good style: it is simple and it
works well for me :-)

2. All code accesses the singleton application object using the app()
method in leoGlobals.

3. To make the code cleaner looking, I would really like the code to be
able to access an app global instead of the app() function. This has
nothing to do with speed: it''s simply that there are lots of references to
app() in the code, and there is soon going to be a lot more references to
app.gui.x() and app.gui.y() etc.

As a workaround, many methods and functions assign a = app(), but I would
like to avoid this step.

4. Alas, it does not seem possible to initialize an app global in
leoGlobals. The reason is simple:

from leoGlobals import *

caches the value of app at the time the import is done. But the startup
code that creates the app "global" (really an attribute of the leoGlobals
module) must do _other_ imports.

For example, doing the following at the top of leoGlobals doesn''t work:

import leoApp
app = leoApp.leoApp() # construct the app instance.

Indeed, the leoApp module will be imported before the assignment of app.
Moreover, the leoApp module does other imports, and all the app variables in
those modules will be uninitialized.

It would be horrible style to place detailed constraints on the order in
which modules get imported, and I''m not sure even that would work.

Any ideas for a clean solution? Thanks.

Edward

P.S. The solution must work in 2.1, 2.2 and 2.3.

EKR
--------------------------------------------------------------------
Edward K. Ream email: ed*******@charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------

解决方案

Edward K. Ream wrote:

I''ve just about convinced myself there is no good, clean solution to the
following puzzle. I wonder if you agree.



If I understand you correctly, you do

leoGlobals.py
_app=None
def app():
global _app
if not _app:
# other code
_app = Application()
return _app

leoMain.py
from leoGlobals import *
app = app()
app.mainLoop()

leoOther.py
from leoGlobals import *

app().doSomething()
Does it happen that your program runs without ever creating the Application
instance? If not, there''s no point in lazy creation:

leoGlobals.py
app=None
def createApp():
global app
assert not app
app = Application()

leoMain.py
from leoGlobals import *
createApp()
app.mainLoop()

leoOther.py
from leoGlobals import *
# this works iff you can ensure that leoMain.py is the
# only "entry point" to your application
app.doSomething()
If I missed something, it would probably help if you post similar code
fragments to show where the above scheme would fail to work.

Peter



On Mon, 29 Sep 2003 11:31:05 -0500, "Edward K. Ream"
<ed*******@charter.net> wrote:

Any ideas for a clean solution? Thanks.



I am not sure that I understand what you are saying, but I''ll make a
quick suggestion...

Perhaps ''app'' can be a global variable within ''leoGlobals'', but
assigned the value ''None'' when the module is imported. Your
initialisation code can then reassign the variable at the appropriate
time.

If you refer to ''app.whatever'' while ''app'' is still ''None'' you should
get an exception.

That''s not what most people mean by ''singleton'' of course, but if you
can be sure of your initialisation sequence it can probably do the
job.
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk


Stephen Horne <


这篇关于一个创业难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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