简体   繁体   English

pycharm不会显示对象的所有属性和方法

[英]pycharm does not show all attributes and methods of a object

When I use Python to write opencv, I have got the image object using the imread method, but when I try to use the object, I cannot see any attribute or method of the method. 当我使用Python编写opencv时,我使用imread方法获得了图像对象,但是当我尝试使用该对象时,看不到该方法的任何属性或方法。

Like this 像这样 在此处输入图片说明

When I use iPython or use the dir() method to check, I can see it 当我使用iPython或使用dir()方法进行检查时,可以看到它 在此处输入图片说明

在此处输入图片说明

This happens when PyCharm can't guess the type of the object returned by method - imread() in this case. 当PyCharm无法猜测方法返回的对象的类型时发生这种情况-在这种情况下为imread() Some methods return different types of object based on the input. 某些方法根据输入返回不同类型的对象。 You'd have to take a look into opencv source code to see why it isn't clear what the returned type is. 您必须查看opencv源代码,以了解为什么不清楚返回的类型是什么。 Static analysis of the code detects obvious cases. 静态分析代码可以发现明显的情况。
IPython has already executed the method, so it's clear what type was returned. IPython已经执行了该方法,因此很清楚返回了什么类型。
One solution, if you know the type returned, is to use type comments like this: 如果您知道返回的类型,一种解决方案是使用如下类型的注释:

import cv2
import numpy
# I've checked with IPython that the returned object is a `numpy.ndarray` instance

img = cv2.imread('/home/me/Pictures/image.jpg')  # type: numpy.ndarray

And then if you type img. 然后,如果您键入img. you will see 你会看见 在此处输入图片说明

The feauture is described on PEP 0484 . 该功能在PEP 0484上进行了描述。
This PEP says it's introduced in Python 3.5. 该PEP表示它是Python 3.5中引入的。 However it might be that PyCharm could handle this simple case in older Python versions than 3.5 but I haven't check. 但是,PyCharm可能可以在比3.5版更旧的Python版本中处理这种简单情况,但我没有检查。
This PEP describes features of typing module which is not available in older Python versions, so most of the features from this document won't work but I'm not sure if PyCharm is really using typing module to parse type comments or does it natively. 该PEP描述了typing模块的功能,而该模块在较早的Python版本中不可用,因此本文档中的大多数功能将不起作用,但是我不确定PyCharm是否真的使用typing模块来解析类型注释或本机执行。

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

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