本文介绍了弃用reload()???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> >其他惊喜:弃用reload()




我同意它没有真的像大多数人认为的那样工作,但是如果没有reload()你将如何执行与reload()相同的任务?


这是由del sys.modules [''modulename'']"完成然后执行一个

''import''?

我将reload()用于多种目的,知道它是如何工作/不工作的。 />

Lance Ellinghaus

解决方案





我通常会想出另一种方法 - 主要是从头开始重新加载整个

程序。在任何情况下,这对我来说都不是一个真正的问题 -

它只是让PythonWin编辑器做到这一点的烦恼

我保持一个命令行打开而不是使用什么(对我而言)

真正破坏了运行的实现。


我希望生活中有很多东西我希望他们按照广告宣传的方式工作。他们不是,所以你应付。


John Roth
Lance Ellinghaus








看起来像reload()可以* *通过扫描当前

加载的模块来处理重新加载模块中对象的所有引用,

并将它们重置为新对象。如果新模块中缺少某个对象,则抛出异常。


GvR建议使用''exec'作为替代方案。


我不知道如何解决消除

旧物件的引用的问题。


- 戴夫





啊,有趣!我一直这样做:


del modulename;导入模块名


但是它没有收到

modulename.py文件的最新更改。


这是比退出Python更好的解决方案和

再次启动它。谢谢!


-

Steven D''Aprano


> > Other surprises: Deprecating reload()



I agree that it does not really work as most people think it does, but how
would you perform the same task as reload() without the reload()?

Would this be done by "del sys.modules[''modulename'']" and then perform an
''import''?

I use reload() for many purposes, knowing how it works/does not work.

Lance Ellinghaus

解决方案




I agree that it does not really work as most people think it does, but how
would you perform the same task as reload() without the reload()?

Would this be done by "del sys.modules[''modulename'']" and then perform an
''import''?

I use reload() for many purposes, knowing how it works/does not work.



I usually figure out another way to do it - mostly reload the entire
program from scratch. In any case, it''s not a real issue for me -
it''s just enough of an irritation that the PythonWin editor does it that
I keep a command line open rather than use what is (to me) a
really broken implementation of run.

There are a lot of things in life that I wished worked the way they
are advertised. They don''t, so you cope.

John Roth
Lance Ellinghaus







I agree that it does not really work as most people think it does, but how
would you perform the same task as reload() without the reload()?



Seems like reload() could be *made* to work by scanning currently
loaded modules for all references to objects in the reloaded module,
and resetting them to the new objects. If an object is missing from
the new module, throw an exception.

GvR suggests using ''exec'' as an alternative.
http://python.org/doc/essays/ppt/regrets/6
I don''t see how that solves the problem of eliminating references to
old objects.

-- Dave




Ah, interesting! I''ve been doing this:

del modulename; import modulename

but it doesn''t pick up any recent changes to the
modulename.py file.

That''s a better solution than exiting Python and
starting it up again. Thanks!

--
Steven D''Aprano


这篇关于弃用reload()???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:58