Skip to content

Commit a625957

Browse files
added the decorator demos...
1 parent 9720ee9 commit a625957

10 files changed

Lines changed: 1075 additions & 0 deletions

week-03/code/StringFormatDemo.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
# <nbformat>3.0</nbformat>
3+
4+
# <codecell>
5+
6+
fp = 3.4
7+
complex = 3+4j
8+
9+
# <codecell>
10+
11+
print "%f"%(fp)
12+
13+
# <codecell>
14+
15+
print "%f, %f"%(fp, complex)
16+
17+
# <codecell>
18+
19+
print "%f, %f+%fj"%(fp, complex.real, complex.imag)
20+
21+
# <markdowncell>
22+
23+
# But what if you don't know what kind of object you need to format in your string?
24+
25+
# <codecell>
26+
27+
print "%s"%("The string formatter")
28+
29+
# <codecell>
30+
31+
# works for anything...
32+
"%s, %s"%(fp, complex)
33+
34+
# <markdowncell>
35+
36+
# What it does is call the __str__ method on the object.
37+
#
38+
# There is also "%r" which calls the __repr__ method.
39+
40+
# <codecell>
41+
42+
"%r, %r"%(fp, complex)
43+
44+
# <codecell>
45+
46+
class test(object):
47+
def __str__(self):
48+
return "This is the ouput of the __str__ method"
49+
def __repr__(self):
50+
return "This is the ouput of the __repr__ method"
51+
52+
# <codecell>
53+
54+
t = test()
55+
"%s"%t
56+
57+
# <codecell>
58+
59+
"%r"%t
60+
61+
# <codecell>
62+
63+

0 commit comments

Comments
 (0)