File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 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' )))
Original file line number Diff line number Diff line change 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' )))
You can’t perform that action at this time.
0 commit comments