为什么使用apt.Cache而不是apt.cache.Cache()创建对象 [英] Why object is created using apt.Cache rather than apt.cache.Cache()

查看:112
本文介绍了为什么使用apt.Cache而不是apt.cache.Cache()创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了困境,我无法前进,对于这个愚蠢的问题,我们深表歉意.我为此进行了很多搜索,但我不知道自己缺少什么.请帮助我.

i'm stucked at a point and i can't progress, sorry for this silly question. I searched a lot for that but i couldn't know what i am missing. Please help me.

我研究了python中的模块和类.现在,我想使用python和apt进行一些操作.我正在从以下学校学习: http://apt.alioth .debian.org/python-apt-doc/library/apt.cache.html 但是,我不明白,该模块是apt.cache,如页面顶部所示.我期望该对象应该通过编写apt.cache.Cache()创建,但是对象是通过编写apt.Cache()创建的,如下所示.为什么?

I studied modules and classes in python. Now i want to make some operations using python and apt. I'm studying from : http://apt.alioth.debian.org/python-apt-doc/library/apt.cache.html However, i couldn't understand, the module is apt.cache, as shown in the top of the page. I expected that object should be created by writing apt.cache.Cache(), but object is created by writing apt.Cache(), as shown below. Why?

    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())

第二个类似的问题与下面的代码有关,Cache类是从模块apt.cache导入的.我希望可以通过编写apt.cache.Cache()创建该对象,但是可以通过编写apt.Cache()创建该对象.为什么?

Second similar question is about below code, Cache class is imported from module apt.cache. I expected that object would be created by writing apt.cache.Cache(), but it is created by writing apt.Cache(). Why?

    >>> 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

预先感谢

推荐答案

如果您查看apt

If you look at the __init__.py file of the apt package, you see the line:

__all__ = ['Cache', 'Cdrom', 'Package']

python 文档说:

import语句使用以下约定:如果软件包的 __ init__.py代码定义了一个名为 all 的列表,它是从包导入时应导入的模块名称的列表 *.

The import statement uses the following convention: if a package’s __ init__.py code defines a list named all, it is taken to be the list of module names that should be imported when from package import * is encountered.

这就是为什么您可以使用apt.Cache()

That's the reason why you can use apt.Cache()

对于问题的第二部分,您可以直接使用

For the second part of your question you can import directly the Cache class with

from apt.cache import Cache
cache = Cache()

您还可以使用以下方式导入Cache类

You can also import the Cache class with

import apt
cache = apt.Cache()       //because of the __all__ variable in __init__.py
cache = apt.cache.Cache() //because it's a fully qualified name

这篇关于为什么使用apt.Cache而不是apt.cache.Cache()创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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