简体   繁体   English

Python:告诉我的 IDE 对象是什么类型

[英]Python: tell my IDE what type an object is

When I write a function in Python (v2.7), I very often have a type in mind for one of the arguments.当我用 Python (v2.7) 编写函数时,我经常会想到其中一个参数的类型。 I'm working with the unbelievably brilliant pandas library at the movemement, so my arguments are often 'intended' to be pandas.DataFrame s.我在运动中使用了令人难以置信的出色pandas库,所以我的论点通常“打算”是pandas.DataFrame s。

In my favorite IDE (Spyder), when you type a period .在我最喜欢的 IDE (Spyder) 中,当您键入句点. a list of methods appear.出现方法列表。 Also, when you type the opening parenthesis of a method, the docstring appears in a little window.此外,当您键入方法的左括号时,文档字符串会出现在一个小窗口中。

But for these things to work, the IDE has to know what type a variable is.但是要使这些事情起作用,IDE 必须知道变量是什么类型。 But of course, it never does.但当然,它永远不会。 Am I missing something obvious about how to write Pythonic code (I've read Python Is Not Java but it doesn't mention this IDE autocomplete issue.我是否遗漏了一些关于如何编写 Pythonic 代码的明显内容(我已经阅读了Python Is Not Java,但它没有提到这个 IDE 自动完成问题。

Any thoughts?有什么想法吗?

I don't know if it works in Spyder, but many completion engines (eg Jedi) also support assertions to tell them what type a variable is.我不知道它是否适用于 Spyder,但许多完成引擎(例如 Jedi)也支持断言来告诉他们变量是什么类型。 For example:例如:

def foo(param):
    assert isinstance(param, str)
    # now param will be considered a str
    param.|capitalize
           center
           count
           decode
           ...

Actually I use IntelliJ idea ( aka pyCharm ) and they offer multiple ways to specify variable types:实际上我使用IntelliJ 想法(又名 pyCharm ) ,它们提供了多种方法来指定变量类型:

1. Specify Simple Variable 1. 指定简单变量

Very simple: Just add a comment with the type information behind the definition.很简单:只需在定义后面添加带有类型信息的注释即可。 From now on Pycharm supports autcompletition! Pycharm 即日起支持自动补全! eg:例如:

def route():
    json = request.get_json() # type: dict

Source: https://www.jetbrains.com/help/pycharm/type-hinting-in-pycharm.html来源: https : //www.jetbrains.com/help/pycharm/type-hinting-in-pycharm.html

2. Specify Parameter : 2. 指定参数

Add three quote signs after the beginning of a method and the idea will autocomplete a docstring, as in the following example:在方法开头后添加三个引号,这个想法将自动完成一个文档字符串,如下例所示:

方法参数示例

Source: https://www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html来源: https : //www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html

(Currently on my mobile, going to make it pretty later) (目前在我的手机上,稍后会制作)

If you're using Python 3, you can use function annotations .如果您使用的是 Python 3,则可以使用函数注释 As an example:举个例子:

@typechecked
def greet(name: str, age: int) -> str:
    print("Hello {0}, you are {1} years old".format(name, age))

I don't use Spyder, but I would assume there's a way for it to read the annotations and act appropriately.我不使用 Spyder,但我认为它有一种方法可以读取注释并采取适当的行动。

I don't know whether Spyder reads docstrings, but PyDev does:我不知道 Spyder 是否读取文档字符串,但 PyDev 会:

http://pydev.org/manual_adv_type_hints.html http://pydev.org/manual_adv_type_hints.html

So you can document the expected type in the docstring, eg as in:所以你可以在文档字符串中记录预期的类型,例如:

def test(arg):
    'type arg: str'
    arg.<hit tab>

And you'll get the according string tab completion.你会得到相应的字符串选项卡完成。

Similarly you can document the return-type of your functions, so that you can get tab-completion on foo for foo = someFunction() .同样,您可以记录函数的返回类型,以便您可以在foofoo = someFunction()获得制表符foo = someFunction()

At the same time, docstrings make auto-generated documention much more helpful.同时,文档字符串使自动生成的文档更有帮助。

The problem is with the dynamic features of Python, I use Spyder and I've used a lot more of python IDEs (PyCharm, IDLE, WingIDE, PyDev, ...) and ALL of them have the problem you stated here.问题在于 Python 的动态特性,我使用 Spyder,并且我使用了更多的 Python IDE(PyCharm、IDLE、WingIDE、PyDev 等),并且所有这些都存在您在此处所述的问题。 So, when I want code completion for help I just instantiate the variable to the type I want and then type ".", for example: suppose you know your var df will be a DataFrame in some piece of code, you can do this df = DataFrame() and for now on the code completion should work for you just do not forget to delete (or comment) the line df = DataFrame() when you finish editing the code.因此,当我需要代码完成以寻求帮助时,我只需将变量实例化为我想要的类型,然后键入“.”,例如:假设您知道您的 var df将是DataFrame代码中的DataFrame ,您可以执行此df = DataFrame()并且现在代码完成应该对您df = DataFrame() ,只是不要忘记在完成代码编辑后删除(或注释) df = DataFrame()行。

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

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