Data Science Tips and Tricks

Alex H. Macy
3 min readDec 29, 2020

In Python, every object has a docstring. A docstring is a helpful bit of text that can give us more insight into how an object operates, along with the functions or methods that can be operated upon or with it. To find the docstring of an object in Python, we can use the help() function. help() takes an object as its positional argument. So, for example, help(len) would return the docstring for the len object. The results of a docstring will include an object’s methods, and what those methods ultimately do. An alternative way of accessing a dostring is to use a question mark, ?, in place of help(). A ? symbol after an object (len?) will return the same results as placing that object in help(). In addition to the dostrings that come natively in Python, the language also gives the user the ability to write their own custom docstrings. This can be achieved using triple quotes inside a function’s definition. So, for example, if we were to write a function that added two discrete variables, say a and b, we could use the triple quote syntax inside of our function (interpreted as a comment). When using help() or ? with our function object, Python would then return our custom docstring.

Another handy trick Python provides for users is to locate the source code where a docstring is referenced. In our previous example, if we were to write our function adding variables a and b, add our docstring to the function, and then lose track of our function in a very long script, we could call our function with two question marks, ??, to take us to the exact location of our specific line of code. That is because ?? returns the section of code where a docstring is defined. Used in conjunction with the dir() method, another convenient tool, we can find source code, as well as all of the attributes and methods for that code as defined in our script.

Sometimes, however, it’s not an end all be all situation, and we want something with a bit more focus. For example, sometimes we may want to find out only the attributes of an object. This can be accomplished by typing the object’s name, followed by tab. This is known as tab completion, and it is very useful in programming because it allows Python to automatically call defined objects, functions, methods, etc. If a function begins with double underscores, however, these functions cannot be tab completed the usual way. These are referred to as dunders, and they exist to perform the implementation, or action, of an object between the Python language and the interpreter.

One of the best uses for tab completion is importing. If you have downloaded or installed Python libraries onto your system, when writing a script you can begin the name of a package, followed by tab, for Python to automatically insert the package into your script. If you don’t know the name of a particular package, or you want to see all packages installed on your system, the import statement followed by tab will pull up a list of all available libraries. Sometimes, however, you may not know part of a package name. Like the beginning, or the end, or perhaps even the middle. This is where another trick comes in handy: Wildcard matching. Using the asterisk character *, referred to in Python as the wildcard character, we can list every object in the namespace that may, for example, end with *cv. If we were ever to use tab completion to search for a method, we could also use double wildcard characters, .*locate*, to pull up any any all methods with these words in their implementation.

More to come!

--

--