views:

202

answers:

2

I created a simple class in Python as follows,

from UserDict import UserDict

class Person(UserDict):
def __init__(self,personName=None):
    UserDict.__init__(self)
    self["name"]=personName

In another module I try to instantiate an object of class Person and print its doc and class attributes:

import Person
p = Person.Person("me")
print p.__doc__
print p.__class__

It bothers me to think that doc and class are not in the list of attributes of an instantiated object when I use content assist in Eclipse:

alt text

Why does this happen? In Java, Eclipse shows the complete list of attributes and methods and this helps me a lot in development sometimes when I don't want to look at the Java Docs. I just figure things out using content assist.

+2  A: 

Not sure if anyone outside of the PyDev development team can really help you here, as this basically boils down to a feature question/request.

I'd suggest creating an item on their Feature Request tracker or their bug tracker.

matt b
+1  A: 

EDIT:

Your class Person is a so-called old-style class because it is subclassed from the UserDict class, an old-style class. There are fundamental differences between old-style and new-style (i.e. classes that subclass from object) in the availability and treatment of special attributes. In particular, dir() of an instance of an old-style class does not return __class__, whereas dir() of new-style class instances do, and, undoubtedly, PyDev is displaying the results of dir():

>>> class OldStyle: pass
... 
>>> os = OldStyle(); os.__class__; dir(os)
<class __main__.OldStyle at 0x100412cb0>
['__doc__', '__module__']
>>> class NewStyle(object): pass
... 
>>> ns = NewStyle(); ns.__class__; dir(ns)
<class '__main__.NewStyle'>
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

As described in recent Python Standard Library documentation, the need for UserDict has largely gone away since, with the introduction of new-style classes in Python 2.2, it is now possible to subclass directly from built-in types like dict. There are other disadvantages of using old-style classes and they have been removed entirely in Python 3, along with the UserDict module. You could get the benefits now, and get better info in PyDev, by changing the Person class to subclass directly from dict.

Ned Deily
doc is there alright but I don't see class anywhere in the list.
Jeune
Ah, right. Forget the original answer: the problem and solution is simpler. See the edited answer.
Ned Deily
Oh I see. That was a mouthful! Thanks a lot. I am reading Dive into Python and using Python 2.6 and this is where I learned to use old style classes. Do I you recommend I read Dive into Python 3 and use Python 3 instead?
Jeune
Heh. Regarding Python 2 vs 3, you'll find lots of opinions about which to use. At the moment, Python 3.1 by itself is pretty solid and usable. The biggest issue is that many popular 3rd-party Python packages (libraries, frameworks, apps, etc) have not yet been ported to Python 3. If you don't anticipate needing any of those right away, you might want to start with Python 3. But the differences between the two aren't all that huge; besides adding some nice features (UNICODE everywhere), Python 3 was an opportunity to get rid of some old cruft, like old-style classes.
Ned Deily