Skip to content

Commit 969acfa

Browse files
author
Rick Riehle
committed
ClassMethods notebook
1 parent 19a390e commit 969acfa

File tree

1 file changed

+269
-0
lines changed

1 file changed

+269
-0
lines changed

notebooks/ClassMethods.ipynb

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 12,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"class Classy(object):\n",
12+
" x = 2\n",
13+
" \n",
14+
" @classmethod\n",
15+
" def class_method(cls, y):\n",
16+
" print(\"In class_method: \", cls)\n",
17+
" return y ** cls.x\n",
18+
" \n",
19+
" def bound_method(self):\n",
20+
" print(\"In bound_method\")\n",
21+
" \n",
22+
" def unbound_method():\n",
23+
" print(\"In unbound_method\")"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 13,
29+
"metadata": {
30+
"collapsed": false
31+
},
32+
"outputs": [
33+
{
34+
"data": {
35+
"text/plain": [
36+
"<bound method Classy.class_method of <class '__main__.Classy'>>"
37+
]
38+
},
39+
"execution_count": 13,
40+
"metadata": {},
41+
"output_type": "execute_result"
42+
}
43+
],
44+
"source": [
45+
"Classy.class_method"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 14,
51+
"metadata": {
52+
"collapsed": false
53+
},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"In class_method: <class '__main__.Classy'>\n"
60+
]
61+
},
62+
{
63+
"data": {
64+
"text/plain": [
65+
"16"
66+
]
67+
},
68+
"execution_count": 14,
69+
"metadata": {},
70+
"output_type": "execute_result"
71+
}
72+
],
73+
"source": [
74+
"Classy.class_method(4)"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 15,
80+
"metadata": {
81+
"collapsed": true
82+
},
83+
"outputs": [],
84+
"source": [
85+
"class SubClassy(Classy):\n",
86+
" x = 3"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 16,
92+
"metadata": {
93+
"collapsed": false
94+
},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"In class_method: <class '__main__.Classy'>\n"
101+
]
102+
},
103+
{
104+
"data": {
105+
"text/plain": [
106+
"4"
107+
]
108+
},
109+
"execution_count": 16,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
114+
"source": [
115+
"Classy.class_method(2)"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 17,
121+
"metadata": {
122+
"collapsed": false
123+
},
124+
"outputs": [
125+
{
126+
"data": {
127+
"text/plain": [
128+
"<function __main__.Classy.bound_method>"
129+
]
130+
},
131+
"execution_count": 17,
132+
"metadata": {},
133+
"output_type": "execute_result"
134+
}
135+
],
136+
"source": [
137+
"Classy.bound_method"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 18,
143+
"metadata": {
144+
"collapsed": true
145+
},
146+
"outputs": [],
147+
"source": [
148+
"my_classy = Classy()"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 19,
154+
"metadata": {
155+
"collapsed": false
156+
},
157+
"outputs": [
158+
{
159+
"data": {
160+
"text/plain": [
161+
"<bound method Classy.bound_method of <__main__.Classy object at 0x1058b53c8>>"
162+
]
163+
},
164+
"execution_count": 19,
165+
"metadata": {},
166+
"output_type": "execute_result"
167+
}
168+
],
169+
"source": [
170+
"my_classy.bound_method"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 20,
176+
"metadata": {
177+
"collapsed": false
178+
},
179+
"outputs": [
180+
{
181+
"data": {
182+
"text/plain": [
183+
"<function __main__.Classy.unbound_method>"
184+
]
185+
},
186+
"execution_count": 20,
187+
"metadata": {},
188+
"output_type": "execute_result"
189+
}
190+
],
191+
"source": [
192+
"Classy.unbound_method"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 21,
198+
"metadata": {
199+
"collapsed": false,
200+
"scrolled": true
201+
},
202+
"outputs": [
203+
{
204+
"ename": "TypeError",
205+
"evalue": "unbound_method() takes 0 positional arguments but 1 was given",
206+
"output_type": "error",
207+
"traceback": [
208+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
209+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
210+
"\u001b[0;32m<ipython-input-21-75cc38280015>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_classy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munbound_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
211+
"\u001b[0;31mTypeError\u001b[0m: unbound_method() takes 0 positional arguments but 1 was given"
212+
]
213+
}
214+
],
215+
"source": [
216+
"my_classy.unbound_method()"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": 22,
222+
"metadata": {
223+
"collapsed": false
224+
},
225+
"outputs": [
226+
{
227+
"name": "stdout",
228+
"output_type": "stream",
229+
"text": [
230+
"In unbound_method\n"
231+
]
232+
}
233+
],
234+
"source": [
235+
"Classy.unbound_method()"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": null,
241+
"metadata": {
242+
"collapsed": true
243+
},
244+
"outputs": [],
245+
"source": []
246+
}
247+
],
248+
"metadata": {
249+
"kernelspec": {
250+
"display_name": "Python 3",
251+
"language": "python",
252+
"name": "python3"
253+
},
254+
"language_info": {
255+
"codemirror_mode": {
256+
"name": "ipython",
257+
"version": 3
258+
},
259+
"file_extension": ".py",
260+
"mimetype": "text/x-python",
261+
"name": "python",
262+
"nbconvert_exporter": "python",
263+
"pygments_lexer": "ipython3",
264+
"version": "3.5.1"
265+
}
266+
},
267+
"nbformat": 4,
268+
"nbformat_minor": 0
269+
}

0 commit comments

Comments
 (0)