ch17: update from book draft
This commit is contained in:
@@ -1,7 +1,18 @@
|
||||
"""
|
||||
Sentence: access words by index
|
||||
|
||||
>>> text = 'To be, or not to be, that is the question'
|
||||
>>> s = Sentence(text)
|
||||
>>> len(s)
|
||||
10
|
||||
>>> s[1], s[5]
|
||||
('be', 'be')
|
||||
>>> s
|
||||
Sentence('To be, or no... the question')
|
||||
|
||||
"""
|
||||
|
||||
# tag::SENTENCE_SEQ[]
|
||||
import re
|
||||
import reprlib
|
||||
|
||||
@@ -17,8 +28,10 @@ class Sentence:
|
||||
def __getitem__(self, index):
|
||||
return self.words[index] # <2>
|
||||
|
||||
def __len__(self, index): # <3>
|
||||
def __len__(self): # <3>
|
||||
return len(self.words)
|
||||
|
||||
def __repr__(self):
|
||||
return 'Sentence(%s)' % reprlib.repr(self.text) # <4>
|
||||
|
||||
# end::SENTENCE_SEQ[]
|
||||
|
||||
Reference in New Issue
Block a user