How to see the methods or attributes of an object in Python?

10

Given an object in Python, I can easily list what methods or attributes it has directly in the interactive interpreter?

    
asked by anonymous 05.11.2015 / 23:55

3 answers

7

Just use the dir () function. An example of its usage:

dir(objeto)
    
06.11.2015 / 10:05
7

There are a few ways to display these attributes. Considering obj being the object you want to know more (being it a declared variable or just a declared class).

  • The simplest and most common is dir :

    dir(obj)
    
  • If you want to know not only the attributes but their current values you can also use __dict__ :

    obj.__dict__
    
  • Finally, you always have the option to consult the help:

    help(obj)
    
07.11.2015 / 04:33
4

It's important that you know the python terminal. This will greatly help your life.

sudo apt-get install ipython.

You can install on virtualenv too:

pip install ipython

Type ipython in the terminal.

With this you can import anything from the python or class of your project.

from people.models import Person
#digitando 'Person.' e apertando tab, você verá todos os atributos da classe Person. Veja a imagem do link

This will prevent you from doing things "blindly" and will save a lot of time from your work.

link

    
06.11.2015 / 15:00