【问题标题】:AttributeError:'module' object has no attribute indexingAttributeError:'module' 对象没有属性索引
【发布时间】:2015-05-26 15:46:26
【问题描述】:

我正在尝试在另一个 python 脚本ProcesosContinuos.py 中使用某个 python news_Lucene.py 脚本的所有功能。我在ProcesosContinuous.py 中导入了news_Lucene.py 的以下结构。

news_Lucene() 的代码:

class news_Lucene():

    @staticmethod
    def indexing():
         #some code

    @staticmethod
    def get_set_terms(reader):
         #some code

    @staticmethod
    def get_TFIDF():
         #some code

    @staticmethod
    def get_author_TFIDF():
         #some code
#SOME CODE OUTSIDE STATIC METHODS 

ProcesosContinuous.py 我正在尝试以下操作:

from engines.extras.download_datos_desambiguar import news_Lucene

news_Lucene().indexing()

我也试过了

indexing()

但它仍然不起作用。它说AttributeError:'module' object has no attribute indexing。我做错了什么?

【问题讨论】:

    标签: python function import lucene attributeerror


    【解决方案1】:

    您导入了模块,而不是模块中包含的类对象。

    用途:

    news_Lucene.news_Lucene().indexing()
    

    或导入类:

    from engines.extras.download_datos_desambiguar.news_Lucene import news_Lucene
    

    Python styleguide 建议您只使用 lowercase_characters_with_underscores 作为模块名称,使用 CamelCase 作为类名;这有助于避免混淆您的模块和类。

    为了遵守风格指南,重命名你的模块和你的类;模块名可以是news_lucene,类名可以是NewsLucene

    我注意到你的整个类只包含静态方法。如果这是该类的唯一目的,您需要完全删除该类。只需将所有内容都设为顶级函数即可。 Python 不是 Java,如果这对您手头的问题没有意义,您根本不需要使用类。

    【讨论】:

    • 谢谢!但是你能告诉我我应该怎么做才能一次调用news_Lucene()类的所有函数吗?而不是一一调用?
    • @kaushaya:创建一个调用其他函数的函数?
    • 在我的news_Lucene() 代码中,f3() 调用f2()。所以如果我只打电话给f3(),它就能访问f2()吗?
    • @kaushaya:模块中的函数可以访问同一模块中的所有其他函数。
    • 静态方法之外还有一些代码。我如何访问它?
    猜你喜欢
    • 2021-01-15
    • 2015-05-16
    • 1970-01-01
    • 2022-12-20
    • 2011-05-13
    • 2018-01-14
    • 2014-08-15
    • 2016-07-17
    • 2016-02-07
    相关资源
    最近更新 更多