Given an object in Python, I can easily list what methods or attributes it has directly in the interactive interpreter?
Given an object in Python, I can easily list what methods or attributes it has directly in the interactive interpreter?
Just use the dir () function. An example of its usage:
dir(objeto)
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)
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.