|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### Callable Class Attributes" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "Class attributes can be any object type, including callables such as functions:" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "class Program:\n", |
| 24 | + " language = 'Python'\n", |
| 25 | + " \n", |
| 26 | + " def say_hello():\n", |
| 27 | + " print(f'Hello from {Program.language}!')" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 2, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "data": { |
| 37 | + "text/plain": [ |
| 38 | + "mappingproxy({'__module__': '__main__',\n", |
| 39 | + " 'language': 'Python',\n", |
| 40 | + " 'say_hello': <function __main__.Program.say_hello()>,\n", |
| 41 | + " '__dict__': <attribute '__dict__' of 'Program' objects>,\n", |
| 42 | + " '__weakref__': <attribute '__weakref__' of 'Program' objects>,\n", |
| 43 | + " '__doc__': None})" |
| 44 | + ] |
| 45 | + }, |
| 46 | + "execution_count": 2, |
| 47 | + "metadata": {}, |
| 48 | + "output_type": "execute_result" |
| 49 | + } |
| 50 | + ], |
| 51 | + "source": [ |
| 52 | + "Program.__dict__" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "As we can see, the `say_hello` symbol is in the class dictionary.\n", |
| 60 | + "\n", |
| 61 | + "We can also retrieve it using either `getattr` or dotted notation:" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": 3, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "data": { |
| 71 | + "text/plain": [ |
| 72 | + "(<function __main__.Program.say_hello()>,\n", |
| 73 | + " <function __main__.Program.say_hello()>)" |
| 74 | + ] |
| 75 | + }, |
| 76 | + "execution_count": 3, |
| 77 | + "metadata": {}, |
| 78 | + "output_type": "execute_result" |
| 79 | + } |
| 80 | + ], |
| 81 | + "source": [ |
| 82 | + "Program.say_hello, getattr(Program, 'say_hello')" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "And of course we can call it, since it is a callable:" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 4, |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [ |
| 97 | + { |
| 98 | + "name": "stdout", |
| 99 | + "output_type": "stream", |
| 100 | + "text": [ |
| 101 | + "Hello from Python!\n" |
| 102 | + ] |
| 103 | + } |
| 104 | + ], |
| 105 | + "source": [ |
| 106 | + "Program.say_hello()" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": 5, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "name": "stdout", |
| 116 | + "output_type": "stream", |
| 117 | + "text": [ |
| 118 | + "Hello from Python!\n" |
| 119 | + ] |
| 120 | + } |
| 121 | + ], |
| 122 | + "source": [ |
| 123 | + "getattr(Program, 'say_hello')()" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "We can even access it via the namespace dictionary as well:" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 6, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [ |
| 138 | + { |
| 139 | + "name": "stdout", |
| 140 | + "output_type": "stream", |
| 141 | + "text": [ |
| 142 | + "Hello from Python!\n" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
| 146 | + "source": [ |
| 147 | + "Program.__dict__['say_hello']()" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [], |
| 155 | + "source": [] |
| 156 | + } |
| 157 | + ], |
| 158 | + "metadata": { |
| 159 | + "kernelspec": { |
| 160 | + "display_name": "Python 3", |
| 161 | + "language": "python", |
| 162 | + "name": "python3" |
| 163 | + }, |
| 164 | + "language_info": { |
| 165 | + "codemirror_mode": { |
| 166 | + "name": "ipython", |
| 167 | + "version": 3 |
| 168 | + }, |
| 169 | + "file_extension": ".py", |
| 170 | + "mimetype": "text/x-python", |
| 171 | + "name": "python", |
| 172 | + "nbconvert_exporter": "python", |
| 173 | + "pygments_lexer": "ipython3", |
| 174 | + "version": "3.6.7" |
| 175 | + } |
| 176 | + }, |
| 177 | + "nbformat": 4, |
| 178 | + "nbformat_minor": 2 |
| 179 | +} |
0 commit comments