2017-04-10 38 views
0
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') 

的list.search( '如何')返回AttributeError的:__eq__重载方法不能正常工作


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' 

你能解释一下,请,什么是错在__ __均衡方法在StringClass中?

+1

您创建了一个'StringClass'实例列表,但是您搜索了一个'str'。 (为什么你甚至没有任何这些类?) – user2357112

+2

错误信息实际上是相当具有描述性的:你用''how'''调用搜索,这是'str'。字符串没有'.str_value'属性(你的字符串类有),你的'.__ eq __()'试图访问它。 – dhke

回答

1

您需要比较两个相同类型的项目。您尝试比较StringClass和内置str。当python尝试读取内置字符串的属性str_value(这是它不具有的属性)时,会发生失败点。

而是将StringClass转换为search函数。

list.search(StringClass('how')) 

或执行内部search该转换,虽然这则意味着不能传递StringClass到函数。

另外,您不应该调用任何变量list。这是一种内置类型,在声明之后,您隐藏了内置版本list

+0

是否有可能在类StringClass的__eq__方法中执行转换? –

+0

无论你喜欢什么,你都可以做到。虽然这不会让我成为一个好地方。主要是因为它限制了'__eq__'函数的允许输入并改变它的语义。任何使用它的人都希望'__eq__'比较像'StringClass'的'StringClass'。如果突然它必须是左边的'StringClass'和右边的'str',那么当你忘记这个区别时,你可能会在后面遇到一些神秘的错误。 –