T.N.Olsthoorn, Feb 27, 2017
Getting the signature of a function or method, getting the methods associated with a class and the attributes of a module is importand and wanted all the time. Searchting the internet and its forums for manuals, documents, tutorials and answers to specific question is often also required, and a requesting the answer for your specific question is often faster done and answer obtained on a forum than looked up in book or a pdf.file, it still comes handy to have tools and techiques readily available for introspection.
A dir(obj) works, but seems a mess at first sight. This is however only the case until you see how to deal with it. A simple list comprehension can filter out exactly what you need into a nice list.
In Ipython (and, therefore also in this notebook), the first way of introspection is tab completion.
Start typing the name of the object and press tap. You can do that at any position of the screen you're busy typing. Python will show a list of available options.
If you press enter do that at the dot like str.<tab> you get a list of all the attributes of the objects.
There are many and you can scroll through them. Try it
In [69]:
#str.<tab> # remove the `#` and the text <tab> and press <tab>
Once the sought one found, press <tab> again to see the attributes associated with the particular choice.
In [70]:
from scipy import special
# try in steps
#scipy.special.airy.identity.conjugate
In [71]:
#str.islower?<shift><enter>
The help appears in a separate window at the bottom of the screen. Press the small at the corner of that window x to remove it again if it takes up too much space at your screen.
A question mark in the front alsow works.
Two question marks yields the underlying python code if it is available.
Alternatively use help(obj)
In [75]:
help(str.lower)
dir().It's often desired to have an overview of all the attributes of an object, without the "private" ones, i.e. those starting with on or two underscores. One way to do this is with the function dir(). However, dir() tends to spawn an overwhelming mess, at least at first sight. This flood can be handled and ordered, however. An easy way is using a simple list comprehension to filter out all objects that start with the underscore.
In [41]:
# This already works for any object (string, list, function, module etc).
anObj = str
[f for f in dir(anObj) if not f.startswith('_')] # is a useful compact overview of public attributes.
Out[41]:
Sometimes you need to know which attributes of an object are callable and which are not.
Lets take the os.path module as an example, which as both callable and non callable attributes.
One way to do is similar to the above, by using the function getmembers() from the module inspect. This function yields a list of tuples where each tuple contains two items: the name of the attribute and the attribute itself:
In [45]:
from inspect import getmembers
In [48]:
anObj = os.path
[f for f in getmembers(anObj) if not f[0].startswith('_')] # is a useful compact overview of public attributes.
Out[48]:
This is a list of tuples of all public members, where the first itme of each tuple is the name of the attribute and the second is the attribute itself.
To see which attributes are methods and which are not, is just a matter of filtering out the callables. Notice that callable(obj) is a builtin function.
In [49]:
anObj = os.path
[f for f in getmembers(anObj) if not f[0].startswith('_') and callable(f[1])]
Out[49]:
And likewise, we find the attributes that are not callable:
In [51]:
anObj = os.path
[f for f in getmembers(anObj) if not f[0].startswith('_') and not callable(f[1])]
Out[51]:
callable)?Signature info refers to how objects should be called, what parameters should be passed to them and what return objects they return.
This infor can be retrieved on a per object basis using the Ipython trick with the question mark as described above. But we can also use the function singature(callable) from the module inspect.
In [77]:
from inspect import signature
In [87]:
anObj = os.path
[(f[0], str(signature(f[1]))) for f in getmembers(anObj) if not f[0].startswith('_') and callable(f[1])]
Out[87]:
However, according to the documentation of module inspect, not all callables are supported by signature(), due to whch this method is not always applicable.
In [76]:
# globals() this can be very long list that you probably don't want to see directly
Likewise, for the local variables you use locals(). However, if you are in a script, that is, at the highest level of the stack, then both 'globals()' and 'locals()' are the same. Only if you call locals() from within a function will the two be different.
An example, shows, how locals() can be called from within a function.
In [60]:
import math
def myfun(x, y, z):
r = math.sqrt(x**2 + y**2 + z**2)
print(locals())
return r
r = myfun(0.3, 0.2, 0.6)
In [63]:
%who
In [64]:
%whos
Note that this nice way of matlab like inspection of the workspace is only available in ipyton, not in python. Hence it is available in this jupyter notebook.
Getting the signature (way of calling) of an object can be done in ipython by typing a question mark directly after the object. The info then appears at the bottom of the screen. Press the small x to remove it, if it takes up too much of the screen.