Pregunta

Greetings SO,

I have a weird problem that I don't seem to be able to solve:

I am working with the pydev plugin with eclipse Helios on Windows XP (if it matters).

I have a module which contains a class. The __init__ of this class takes a parameter which determines the set of attributes that a method of this class should have.

Since I am not allowed to show actual code, I can give the following analogy:

class Car:
    def __init__(self, attrs):
        # attrs is a dictionary.
        # the keys of attrs are the names of attributes that this car should have
        # for example, a key of attr could be 'tires'

        # the values of attrs are the values of the attributes which are the keys
        # so if the key is 'tires', it's value might be 4

Now, since I'm setting these variables dynamically at runtime, Pydev is not able to give me suggestions when I do this:

c = Car()
print c.tires

When I type in "c." +, pydev does not offer tires as a suggestion.

How might I go about getting this functionality? Or is it just not something that pydev can do at present?

I'd appreciate any help

¿Fue útil?

Solución 2

+1 to Imran for his solution. But, Ia better solution occurred to me:

Create all attributes in `__init__`.
When it comes time to be dynamic, delete the unwanted attributes.

This way, ene though the superset of all attributes is still suggested in the autocomplete, memory is not wasted.

Otros consejos

This is a general problem which all dynamic language IDEs suffer. Pydev has no way of knowing what attributes Car.__init__ sets on instances of Car, without executing your code. If you use class variables for the attributes you set in __init__, Pydev should be able to offer autocomplete suggstion.

class Car(object):
    tires = 4

    def __init__(self, attrs):
         self.tires = attrs.get('tires', self.tires)
         self.tires += attrs.get('spare', 0)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top