Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python django how do you print out an object's introspection? The list of all public methods of that object (variable and/or functions)?

Tags:

python

django

In python django how do you print out an object's inrospection? The list of all public methods of that object (variable and/or functions)?

e.g.:

def Factotum(models.Model):
  id_ref = models.IntegerField()

  def calculateSeniorityFactor():
    return (1000 - id_ref) * 1000

I want to be able to run a command line in the Django shell to tell me all of the public methods of a Django model. The output of running on above would be:

>> introspect Factotoum
--> Variable: id_ref
--> Methods: calculateSeniorityFactor
like image 422
MikeN Avatar asked Jan 29 '10 19:01

MikeN


People also ask

How do I see all methods in Python?

Python – all() function The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.)

How can you see which properties and methods are available for a specific Python object?

You can use the built in dir() function to get a list of all the attributes a module has. Try this at the command line to see how it works. Also, you can use the hasattr(module_name, "attr_name") function to find out if a module has a specific attribute.

What is use of __ str __ function in Django?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model. # Create your models here. This will display the objects as something always in the admin interface.


1 Answers

Well, things you can introspect are many, not just one.

Good things to start with are:

>>> help(object)
>>> dir(object)
>>> object.__dict__

Also take a look at the inspect module in the standard library.

That should make 99% of all the bases belong to you.

like image 179
Lennart Regebro Avatar answered Sep 22 '22 22:09

Lennart Regebro