简体   繁体   中英

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.

Like this 在此处输入图片说明

When I use iPython or use the dir() method to check, I can see it 在此处输入图片说明

在此处输入图片说明

This happens when PyCharm can't guess the type of the object returned by method - imread() in this case. 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. Static analysis of the code detects obvious cases.
IPython has already executed the method, so it's clear what type was returned.
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. you will see 在此处输入图片说明

The feauture is described on PEP 0484 .
This PEP says it's introduced in 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.
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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