{ "cells": [ { "cell_type": "markdown", "id": "faef0c7f-b54b-4841-8a87-713f82633ff6", "metadata": {}, "source": [ "# How do you explore what makes up an object?\n", "\n", "The `dir` built-in function in Python allows one to *explore the contents* of an object.\n", "\n", "\n", "\n", "That is, the `dir` function returns a `list` of an object's *attributes*, which can be either *data* (i.e., variables) or *functions*." ] }, { "cell_type": "markdown", "id": "ab06f6f2-ba94-49f5-84d8-f67f65f3d567", "metadata": { "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "## Everything in Python is an object!\n", "\n", "Don't believe me? Try out the `dir` function on anything you can think of..." ] }, { "cell_type": "markdown", "id": "037b6557-8519-493d-ba11-8f653b902cda", "metadata": { "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "### Example: Strings have attributes" ] }, { "cell_type": "code", "execution_count": null, "id": "afa88f7d-bd5e-4242-93f6-acd2bfd9de17", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [], "source": [ "dir('string')" ] }, { "cell_type": "markdown", "id": "560c1817-77d6-4bf4-b19d-3f7e66dbf8eb", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "source": [ "
\n", " Note:\n", "

Some attributes have names that start with __ (double-underscore). In Python, variables or functions that start with a double-underscore are treated as \"private.\" In general, variables or functions that start with a double-underscore and end with a double-underscore typically have special meaning. We'll get to that later.

\n", "
" ] }, { "cell_type": "markdown", "id": "aa59784d-678d-42a0-9163-471ba3441438", "metadata": { "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "### Example: Numbers have attributes" ] }, { "cell_type": "code", "execution_count": null, "id": "44fc2b6f-b851-4440-8233-f173a0439e0f", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [], "source": [ "dir(1)" ] }, { "cell_type": "markdown", "id": "630529a1-9d08-48d1-bf80-b1e865ee3a75", "metadata": { "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "### Exercise: Try it out with other data types..." ] }, { "cell_type": "code", "execution_count": null, "id": "9e4f7248-e29d-4e89-ada2-c11d724ead44", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [], "source": [ "# What attributes does a list have?" ] }, { "cell_type": "code", "execution_count": null, "id": "447185b6-f454-4d36-9553-83958921b94b", "metadata": {}, "outputs": [], "source": [ "# What attributes does a dict have?" ] }, { "cell_type": "code", "execution_count": null, "id": "e221812d-85d3-4567-9a97-b1b67bfb830b", "metadata": {}, "outputs": [], "source": [ "# What attributes does a function have?" ] }, { "cell_type": "markdown", "id": "0f9e2d8b-eb8e-4677-9309-5dbecddf1656", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "source": [ "
\n", " Question:\n", "

Do you recognize any of the attributes listed above?

\n", "
" ] }, { "cell_type": "markdown", "id": "a46483b3-fc71-457e-88c6-20800710ccb2", "metadata": { "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "## Tab Completion in Jupyter Notebooks!\n", "\n", "Jupyter Notebooks have the wonderful ability to *show* you an object's attributes just by adding a \"dot\" and then hitting tab. Jupyter's built-in **tab completion** capability will then pop up a window to show you the attributes of the object!" ] }, { "cell_type": "markdown", "id": "429809e6-5841-4c54-9b06-7be301aa2e5b", "metadata": {}, "source": [ "### Example: Tab completion to show an integer's attributes" ] }, { "cell_type": "code", "execution_count": null, "id": "bc8c9e18-cec3-4a6a-863f-d18ffbf98e20", "metadata": {}, "outputs": [], "source": [ "i = 2" ] }, { "cell_type": "code", "execution_count": null, "id": "8c6d10e5-ffbf-4f3c-ba9f-7673808a4e99", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [], "source": [ "i." ] }, { "cell_type": "markdown", "id": "dbdc57f8-9565-4006-aa1d-ef9e2fcad0b3", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "source": [ "
\n", " Note:\n", "

Jupyter's visual \"pop-up\" shows an object's public attributes only. If you are using JupyterLab, you will see that each attribute is labeled as either an instance (i.e., data) or a function. If you are using classic Jupyter Notebooks, then you will just see a list of the attributes (without additional labeling).

\n", "
" ] }, { "cell_type": "markdown", "id": "872a9897-599e-4476-a5f1-f73cad882434", "metadata": { "slideshow": { "slide_type": "fragment" }, "tags": [] }, "source": [ "
\n", " Note:\n", "

Jupyter can't give you a pop-up window unless the variable you are using to tab-complete is already defined and created!

\n", "
" ] }, { "cell_type": "markdown", "id": "ce84dbd3-9c9c-4a85-90da-ff60fa52d183", "metadata": {}, "source": [ "| | | |\n", "| :- | -- | -: |\n", "| [[Home]](../index.ipynb) | | [« Previous](01.ipynb) \\| [Next »](03.ipynb) |" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }