Home pycharm does not show all attributes and methods of a object
Reply: 1

pycharm does not show all attributes and methods of a object

Xavier
1#
Xavier Published in 2017-12-07 07:42:35Z

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

ElmoVanKielmo
2#
ElmoVanKielmo Reply to 2017-12-07 08:44:00Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304622 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO