Reputation: 15
class StringClass:
def __init__(self, str_value):
self.str_value = str_value
def __eq__(self, object):
if self.str_value == object.str_value:
return True
else:
return False
def __str__(self):
return str( self.str_value)
def __repr__(self):
return str( self.str_value)
def __getitem__ (self, index):
return self.str_value [ index ]
def __gt__(self, object):
if self.str_value > object.str_value:
return True
else:
return False
class StringListClass:
def __init__ (self, size):
self.size = size # the size of the str_list
self.str_list = [None] * size # the list with an initial size
self.counter = 0 # shows which index in the list is filled
def add (self, new_item):
if self.size - 1 >= self.counter:
self.str_list [ self.counter ] = new_item
self.counter += 1
return print ( 'Element', new_item, 'has been added, the list is :', self.str_list [:self.counter] )
else:
return print ( 'Error! Only', self.size, 'elements can be added into the list' )
def sort (self):
changed = True
while changed:
changed = False
for i in range ( len ( self ) - 1 ):
if ord ( self.str_list [i] [0] ) > ord ( self.str_list [i + 1] [0] ):
self.str_list [i], self.str_list [i + 1] = self.str_list [i + 1], self.str_list [i]
changed = True
return print ( 'Sorted list:', self.str_list [:len ( self )] )
def search (self, target_item):
self.sort ( )
low = 0
high = len ( self ) - 1
while low <= high:
mid = (low + high) // 2
if self.str_list [mid] == target_item:
return print ( 'The element', target_item, 'has been found!' )
elif target_item < self.str_list [mid]:
high = mid - 1
else:
low = mid + 1
return print ( 'The element ', target_item, 'is not listed in the list!' )
def __len__ (self):
return self.counter
def __str__ (self):
string = ""
if len ( self ) != 0:
for i in range ( len ( self ) ):
string += str ( i ) + ' element is: ' + str ( self.str_list [i] ) + "\n"
else:
string = "The list is empty!"
return string
list = StringListClass ( 10 )
a = StringClass ( 'Hello' )
b = StringClass ( 'how' )
c = StringClass ( 'are' )
d = StringClass ( 'you' )
list.add ( a )
list.add ( b )
list.add ( c )
list.add ( d )
list.sort()
list.search('how')
The list.search('how') returns the AttributeError:
AttributeError Traceback (most recent call last)
<ipython-input-40-49ee6e8bf4c7> in <module>()
104
105 list.sort()
--> 106 list.search('how')
107
108 print(a)
<ipython-input-40-49ee6e8bf4c7> in search(self, target_item)
67 mid = (low + high) // 2
68
---> 69 if self.str_list [mid] == target_item:
70 return print ( 'The element', target_item, 'has been found!' )
71 elif target_item < self.str_list [mid]:
<ipython-input-40-49ee6e8bf4c7> in __eq__(self, object)
7 def __eq__(self, object):
8
----> 9 if self.str_value == object.str_value:
10 return True
11 else:
AttributeError: 'str' object has no attribute 'str_value'
Can you explain, please, what is wrong in __ eq __ method in StringClass?
Upvotes: 0
Views: 249
Reputation: 21619
You need compare two items of the same type. You try to compare a StringClass
and an inbuilt str
. The point of failure occurs when python tries to read the property str_value
of the inbuilt string, which is a property it does not have.
Instead pass a StringClass
into the search
function.
list.search(StringClass('how'))
or perform that conversion inside search
, although this then means you can't pass a StringClass
into the function.
As an aside, you should not call any variables list
. It's an inbuilt type and after that declaration, you hide the inbuilt version of list
.
Upvotes: 1