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)?
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
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.
Use inspect:
import inspect
def introspect(something):
methods = inspect.getmembers(something, inspect.ismethod)
others = inspect.getmembers(something, lambda x: not inspect.ismethod(x))
print 'Variable:', # ?! what a WEIRD heading you want -- ah well, w/ever
for name, _ in others: print name,
print
print 'Methods:',
for name, _ in methods: print name,
print
There's no way you can invoke this without parentheses in a normal Python shell, you'll have to use introspect(Factotum)
((with class Factotum
property imported in the current namespace of course)) and not introspect Factotum
with a space. If this irks you terribly, you may want to look at IPython.
精彩评论