简体   繁体   中英

PyDev/Eclipse/Python not showing all methods of a class

Running Eclipse (Galileo) with PyDev. I am trying to work with the module urllib2 and I've just started experimenting with it. One of the tutorials I am going through says to call geturl() on my urllib2.urlopen() class. So I did, and it worked as expected.

However, I was surprised when I tried to browse the class methods by using Eclipses handy-dandy "ctrl+space" shortcut it only gives me the private methods for what I think are a base class:

在此处输入图片说明

When I do dir(page) , I get all sorts of neat methods I can play with!

['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close', 'code', 'fileno', 'fp', 'getcode', 'geturl', 'headers', 'info', 'msg', 'next', 'read', 'readline', 'readlines', 'url']

Is this an Eclipse problem? Or a user error? If I put this in IDLE, I can use IDLE's auto-completion feature to see what methods are available, but I can't get the same functionality to work with PyDev; I believe it is working on my work PC, but my home PC (the one I am at now) seems to be "not normal".

I have looked as best I could on Google/Stackoverflow, but I'm not sure which keywords will return the result that might help me understand what's going wrong here. Any insight you can provide would be great!

(Python 2.6, running in a Debian (#!) virtual environment, windows7 host - if any of that matters)

NB: I'm only developing on the Debian side, so I don't have eclipse on Win7.

UPDATE1:

To answer, "what does the browser end with if I scroll down?" It ends with subclasshook . See the photo below for what the bottom of the list looks like (it's all private , no methods).

在此处输入图片说明

UPDATE2:

As previous post suggested, I went into the preferences menu and checked the autocompletion tabs, and the interpreter preference tab, and nothing seems out of place (but then again, after reading the pydev autocompletion document, I'm not seeing anything about a 'firewall' or any indication that there is one.

However, I did another test, see the image below:

在此处输入图片说明

The auto-completion feature for wx works just fine. Perhaps I misunderstood what was being told to me in previous posts, however, I am understanding that the reason I am seeing magic methods for my page class is because PyDev doesn't know what else to populate it with - so it's obviously confused. I'm just not sure where to go from here. I'll keep reading PyDev docs to see if I can figure out in the meantime.

And, I almost forgot - when I "hover" my mouse over urllib2.urlopen() in my code, it does show me the doc string. Not sure if that provides any insight about this supposed broken functionality.

UPDATE3:

As per someones comment regarding "forced builtins" only auto-completing in PyDev, I went and added "urllib2" to the forced builtins under preferences. Below is the image of what I am getting...

在此处输入图片说明

I have some new methods... but not the ones I expected (ie I am NOT seeing the ones I get from __dict__ when I call dir(page) , specifically, geturl()).

Most of those are Python's "magic methods":

http://www.rafekettler.com/magicmethods.html

They're special methods you can define on any object to give it custom behavior. Most Python IDEs know not to show them, but Eclipse is being especially helpful.

The problem you're running into is a fundamental one with a dynamic language like Python: your IDE has no way of knowing what type of object your page variable refers to. To do that, it would have to know what type urlopen() returns. And there's no way to know that without calling it! It may not even return the same type each time you call it, in which case, what class's methods should it show you?

IDEs can make some guesses, but when these guesses fail, you get results like what you're seeing. It knows you have an object, because everything's an object, so it shows you methods of object .

If you read the documentation :

It's important to note that your interpreter must be properly configured for the code-completion to work, and for getting the builtins, PyDev spawns a shell, so, having a firewall can prevent the code-completion from working if it's blocking the communication from eclipse to that shell.

In other words, you have to go into the Preferences and set both the "Code Completion" settings and the "Interpreter - Python" settings, and make sure Eclipse can start a subshell and communicate with it over a socket without a firewall or anything else getting in the way.

My guess is that, of those things, your interpreter setup is most likely to be wrong. Even if your Python is in the usual /usr/bin/python location, you may have PyDev set up to look for, say, /usr/bin/python3 , or python26 , or "don't guess, always use the #! line or a project-specific setting", or you've installed Python in a per-user location instead of system-wide, or who knows what.

See what you have in the "Interpreter - Python" prefs page, and see if you can run Python the way Eclipse expects to run it. If not, it will not be able to do any command-line completion except the basic stuff that's built in. (As I said elsewhere, I believe that means it can guess that page is of some class type, but it can't tell what type, so it will guess all the standard magic methods that nearly any class object has, and nothing else.)

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