-
Notifications
You must be signed in to change notification settings - Fork 959
/
method_is_descriptor.py
41 lines (33 loc) · 1.08 KB
/
method_is_descriptor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
# tag::FUNC_DESCRIPTOR_DEMO[]
>>> word = Text('forward')
>>> word # <1>
Text('forward')
>>> word.reverse() # <2>
Text('drawrof')
>>> Text.reverse(Text('backward')) # <3>
Text('drawkcab')
>>> type(Text.reverse), type(word.reverse) # <4>
(<class 'function'>, <class 'method'>)
>>> list(map(Text.reverse, ['repaid', (10, 20, 30), Text('stressed')])) # <5>
['diaper', (30, 20, 10), Text('desserts')]
>>> Text.reverse.__get__(word) # <6>
<bound method Text.reverse of Text('forward')>
>>> Text.reverse.__get__(None, Text) # <7>
<function Text.reverse at 0x101244e18>
>>> word.reverse # <8>
<bound method Text.reverse of Text('forward')>
>>> word.reverse.__self__ # <9>
Text('forward')
>>> word.reverse.__func__ is Text.reverse # <10>
True
# end::FUNC_DESCRIPTOR_DEMO[]
"""
# tag::FUNC_DESCRIPTOR_EX[]
import collections
class Text(collections.UserString):
def __repr__(self):
return 'Text({!r})'.format(self.data)
def reverse(self):
return self[::-1]
# end::FUNC_DESCRIPTOR_EX[]