我陷入困境,我无法取得进展,为这个愚蠢的问题感到抱歉。我找了很多,但我不知道我错过了什么。请帮帮我。
我在python中学习了模块和类。现在,我想使用python和apt进行一些操作。我正在学习:http://apt.alioth.debian.org/python-apt-doc/library/apt.cache.html,但是,我无法理解,模块是apt.cache,如页面顶部所示。我期望通过编写apt.cache.Cache()来创建对象,但是对象是通过编写apt.Cache()创建的,如下所示。为什么?
import apt
import apt.progress
# First of all, open the cache
cache = apt.Cache()
# Now, lets update the package list
cache.update()
# We need to re-open the cache because it needs to read the package list
cache.open(None)
# Now we can do the same as 'apt-get upgrade' does
cache.upgrade()
# or we can play 'apt-get dist-upgrade'
cache.upgrade(True)
# Q: Why does nothing happen?
# A: You forgot to call commit()!
cache.commit(apt.progress.TextFetchProgress(),
apt.progress.InstallProgress())第二个类似的问题是关于下面的代码,缓存类是从模块apt.cache导入的。我期望通过编写apt.cache.Cache()来创建这个对象,但是它是通过编写apt.Cache()来创建的。为什么?
>>> from apt.cache import FilteredCache, Cache, MarkedChangesFilter
>>> cache = apt.Cache()
>>> changed = apt.FilteredCache(cache)
>>> changed.set_filter(MarkedChangesFilter())
>>> print len(changed) == len(cache.get_changes()) # Both need to have same length
True提前感谢
发布于 2015-04-13 16:03:15
__all__ = ['Cache', 'Cdrom', 'Package']蟒蛇文档说:
导入语句使用以下约定:如果包的__ init__.py代码定义了一个名为all的列表,则它被视为在遇到package *时应该导入的模块名称列表。
这就是为什么你可以使用apt.Cache()
对于问题的第二部分,您可以使用
from apt.cache import Cache
cache = Cache()您还可以使用
import apt
cache = apt.Cache() //because of the __all__ variable in __init__.py
cache = apt.cache.Cache() //because it's a fully qualified namehttps://stackoverflow.com/questions/28933807
复制相似问题