Skip to content

Commit de7ef46

Browse files
committed
finished most of session 7
1 parent c5b5c86 commit de7ef46

7 files changed

Lines changed: 248 additions & 191 deletions

File tree

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
slides_sources/build
2-
#ignore python compile files
32

4-
#ignore compile files, sublime workspace and project files
3+
#ignore compiled files, sublime workspace and project files
54
*.pyc
65
*.sublime*
76

8-
#ignore changes in Examples directory
9-
10-
/Examples/

Examples/Session07/class_method.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
example of a class method
5+
"""
6+
7+
8+
class C(object):
9+
def __init__(self, x, y):
10+
self.x = x
11+
self.y = y
12+
13+
@classmethod
14+
def a_class_method(cls, y):
15+
print "in a_class_method", cls
16+
return cls(y, y**2)
17+
18+
19+
class C2(C):
20+
pass
21+
22+
23+
if __name__ == "__main__":
24+
25+
c = C(3, 4)
26+
print type(c), c.x, c.y
27+
28+
c2 = C.a_class_method(3)
29+
print type(c2), c2.x, c2.y
30+
31+
c3 = c2.a_class_method(2)
32+
print type(c3), c3.x, c3.y
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Example code for properties
5+
6+
NOTE: if your getters and setters are this simple: don't do this!
7+
8+
"""
9+
10+
11+
class C(object):
12+
_x = None
13+
@property
14+
def x(self):
15+
return self._x
16+
@x.setter
17+
def x(self, value):
18+
self._x = value
19+
@x.deleter
20+
def x(self):
21+
del self._x
22+
23+
if __name__ == "__main__":
24+
c = C()
25+
c.x = 5
26+
print c.x
27+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
examples of a static methods
5+
"""
6+
7+
8+
class C(object):
9+
10+
@staticmethod
11+
def a_static_method(a, b):
12+
print "in a_static_method"
13+
return a+b
14+
15+
def test(self):
16+
return self.a_static_method(2,3)
17+
18+
if __name__ == "__main__":
19+
20+
print C.a_static_method(3,4)
21+
22+
c = C()
23+
24+
print c.a_static_method(4,5)
25+
26+
print c.test()
27+
28+

slides_sources/source/homework/circle_class.rst

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,76 @@ The user should not be able to set the area:
119119
Step 5:
120120
-------
121121

122-
more to come...
122+
Add an "alternate constructor" that lets the user create a Circle directly
123+
with the diameter:
123124

125+
.. code-block:: python
126+
127+
>> c = Circle.from_diameter(8)
128+
>> print c.diameter
129+
8
130+
>> print c.radius
131+
4
132+
133+
Step 6:
134+
-------
135+
136+
Add __str__ and __repr__ methods to your Circle class.
137+
138+
Now you cant print it:
139+
140+
.. code-block:: ipython
141+
142+
In [2]: c = Circle(4)
143+
144+
In [3]: print c
145+
Circle with radius: 4.000000
146+
147+
In [4]: repr(c)
148+
Out[4]: 'Circle(4)'
149+
150+
In [5]: d = eval(repr(c))
151+
152+
In [6]: d
153+
Out[6]: Circle(4)
154+
155+
Step 7:
156+
--------
157+
158+
Add some of the numeric protocol to your Circle:
159+
160+
you should be able to add two circles:
161+
162+
.. code-block:: ipython
163+
164+
In [7]: c1 = Circle(2)
165+
166+
In [8]: c2 = Circle(4)
167+
168+
In [9]: c1 + c2
169+
Out[9]: Circle(6)
170+
171+
and multiply one times a number:
172+
173+
.. code-block:: ipython
174+
175+
In [16]: c2 * 3
176+
Out[16]: Circle(12)
177+
178+
and compare them:
179+
180+
.. code-block:: ipython
181+
In [10]: c1 > c2
182+
Out[10]: False
183+
184+
In [11]: c1 < c2
185+
Out[11]: True
186+
187+
In [12]: c1 == c2
188+
Out[12]: False
189+
190+
In [13]: c3 = Circle(4)
191+
192+
In [14]: c2 == c3
193+
Out[14]: True
124194

slides_sources/source/homework/html_builder.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ HTML Primer
197197
============
198198

199199
.. rst-class:: medium
200-
The very least you need to know about html to do this assigment.
200+
201+
The very least you need to know about html to do this assigment.
201202

202203
If you are familar with html, then this will all make sense to you. If you have
203204
never seen html before, this might be a bit intimidating, but you really don't

0 commit comments

Comments
 (0)