I have a class Sentence with a magic method function __getslice__
I am not understanding how to call that function ?
I am trying to do slicing of words. so for example if the string is "HELLO WORLD, james" , and I slice it at [0:1] , I expect to get "HELLO"
I am getting instead this error: 'method' object is not subscriptable
Here is my code:
class Sentence:
def __init__(self, string):
self._string = string
def getSentence(self):
return self._string
def getWords(self):
return self._string.split()
def getLength(self):
return len(self._string)
def getNumWords(self):
return len(self._string.split())
def capitalize(self):
self._string = self._string.upper()
def punctation(self):
self._string = self._string + ", "
def __str__(self):
return self._string
def __getitem__(self, k):
return self._string[k]
def __len__(self):
return self._String
def __getslice__(self, start, end):
return self[max(0, start):max(0, end):]
def __add__(self, other):
self._string = self._string + other
return self._string
def __frequencyTable__(self, word):
count = 0
for w in self._string:
if self._string.has_key(word):
count = count + 1
return count
def __contains__(self, word):
return word in self._string
def functionTesting():
hippo = Sentence("hello world")
print(hippo.getSentence())
print(hippo.getLength())
print(hippo.getNumWords())
print(hippo.getWords())
hippo.capitalize()
hippo.punctation()
print(hippo.getSentence())
print("HELLO" in hippo) ##contains
print(hippo.__getitem__(0)) ##__getitem
print(hippo.__add__("james")) ##__add__
print(hippo.__getslice__[0:1])
functionTesting()
Also where can I learn more about magic method functions ? As it seems I am having trouble calling the functions more than writing them
When you do hippo.__getslice__[0:1] you are actually trying to slice the method hippo.__getslice__. That is why it is failing with the error
'method' object is not subscriptable
Important Note: __getslice__ is deprecated since Python 2.0 and it is not available in Python 3.x. Quoting the official documentation of __getslice__,
Deprecated since version 2.0: Support slice objects as parameters to the
__getitem__()method. (However, built-in types in CPython currently still implement__getslice__(). Therefore, you have to override it in derived classes when implementing slicing.)
So you should use __getitem__ for slicing. Quoting the question,
for example if the string is "HELLO WORLD, james" , and I slice it at [0:1] , I expect to get "HELLO"
Since you want to get words with slicing, if the key passed to __getitem__ is a slice object, then call self.getWords() and slice the returned object like shown below
def __getitem__(self, k):
if isinstance(k, slice):
return self.getWords()[k]
return self._string[k]
....
print(hippo[0:1])
['HELLO']
Note 1: You don't have to explicitly call __getitem__ when you are subscripting the hippo object. You can simply do
print(hippo[0])
# H
This will internally call __getitem__ with k as 0.
Note 2: Same as the previous one. You don't have to explicitly call __add__ and that can be implicitly called with the arithmetic operator +, like this
print(hippo + "james")
# HELLO WORLD, james
Note 3: In your __frequenceTable__ implementation, you are using has_key method (which is a deprecated dictionary method on a string. So, at runtime, your program will fail with
AttributeError: 'str' object has no attribute 'has_key'
Perhaps you meant to use in operator.
def __frequencyTable__(self, word):
count = 0
for w in self._string:
if word in self._string:
count = count + 1
return count
PS: I am not sure what this __frequencyTable__ method tries to achieve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With