Skip to content

Commit ae36cb7

Browse files
committed
Initial commit of item_03 examples
1 parent e30305e commit ae36cb7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Diff for: item_03_example_03.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python2.7
2+
3+
'''Item 3, example 3, from Effective Python'''
4+
5+
6+
# Example 3
7+
def to_unicode(unicode_or_str):
8+
'''In Python 2, you'll need one method that takes a str or unicode and
9+
always returns a unicode .'''
10+
if isinstance(unicode_or_str, str):
11+
value = unicode_or_str.decode('utf-8')
12+
else:
13+
value = unicode_or_str
14+
return value # Instance of unicode
15+
16+
print(repr(to_unicode(u'foo')))
17+
print(repr(to_unicode('foo')))

Diff for: item_03_example_04.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python2.7
2+
3+
'''Item 3, example 4, from Effective Python'''
4+
5+
6+
# Example 4
7+
def to_str(unicode_or_str):
8+
'''You'll need another method that takes str or unicode and always returns
9+
a str.'''
10+
if isinstance(unicode_or_str, unicode):
11+
value = unicode_or_str.encode('utf-8')
12+
else:
13+
value = unicode_or_str
14+
return value # Instance of str
15+
16+
print(repr(to_str(u'foo')))
17+
print(repr(to_str('foo')))

0 commit comments

Comments
 (0)