Is there a Python function or method that displays the specifications of an object?

Is there a built-in method for getting specification comments in Python that can be used instead of returning and reading them in a class definition? In particular, I am trying to determine how many and what arguments an object takes from an imported module.

I could not find the answer to this in the Python documentation, and if it was already answered, I could not get it in the search.

+4
source share
3 answers
>>> from random import randint
>>> help(randint)


Help on method randint in module random:

randint(self, a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.
(END)

http://docs.python.org/2.7/library/functions.html#help

with custom class:

>>> class Test(object):
...     def __init__(self, smile):
...             print(smile)
...
>>> help(Test)

Help on class Test in module __main__:

class Test(__builtin__.object)
 |  Methods defined here:
 |  
 |  __init__(self, smile)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
(END)
+6
source

You can also use inspect , in particular inspect.getmembers () :

inspect.getmembers(object[, predicate])

(, ), .

:

>>> import inspect
>>> class Test(object):
...     def __init__(self, smile):
...         print(smile)
... 
>>> inspect.getmembers(Test, predicate=inspect.ismethod)
[('__init__', <unbound method Test.__init__>)]
+3

You can try to install an interactive shell ipython and use a macro %pdoc, %pdef:

% pdoc: print (or scroll the pager if it is too long) docstring for the object. If this object is a class, it will print both the constructor’s and constructor’s docs.

% pdef: Print the definition header for any called object. If the object is a class, print constructor information.

+1
source

Source: https://habr.com/ru/post/1533411/


All Articles