简体   繁体   English

Python:Gensim 的类型提示

[英]Python: Type hinting for Gensim

In order to pass my gensim corpus and dictionary through the pipeline, I have made a convenience case class as follows:为了通过管道传递我的 gensim 语料库和字典,我制作了一个方便的案例类,如下所示:

class GensimCorpusAndDict:
    def __init__(self, corpus,  dict):
        self.corpus = corpus
        self.dict = dict

It would be helpful to get type hinting on the input parameters.在输入参数上获得类型提示会很有帮助。 I have read the docs but still not sure how to type hint on third party classes我已经阅读了文档,但仍然不确定如何在第三方类上输入提示

Read thedocs (PEP-0484) :阅读文档(PEP-0484)

Type hints may be [...] and user-defined classes (including those defined in the standard library or third-party modules).类型提示可以是 [...] 和用户定义的类(包括在标准库或第三方模块中定义的类)。

For example:例如:

import numpy as np
import pandas as pd

class GensimCorpusAndDict:
    def __init__(self, corpus: np.ndarray, the_dict: pd.DataFrame):
        self.corpus = corpus
        self.dict = the_dict

Note: it is not good practice to use a variable name that shadows a built-in (in this case dict ).注意:使用隐藏内置变量的变量名(在本例中为dict )不是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM