2017-04-10 8 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' 

あなたは__のEQ __方法で間違っているものを、してください説明することができますStringClassで?

+1

'StringClass'インスタンスのリストを作成しましたが、' str'を検索しました。 (なぜあなたはこれらのクラスを持っているのですか?) – user2357112

+2

エラーメッセージは実際にはかなり記述的です: '' how''、つまり 'str'で検索を呼び出します。文字列はあなたの '.__ eq __()'がアクセスしようとしている '.str_value'属性(あなたの文字列クラスがあります)を持っていません。 – dhke

答えて

1

同じタイプの2つのアイテムを比較する必要があります。 StringClassと組み込みのstrを比較しようとします。障害のポイントは、Pythonがinbuilt文字列のプロパティstr_valueを読み込もうとしたときに発生します。これは持っていないプロパティです。

代わりにStringClasssearch関数に渡します。

list.search(StringClass('how')) 

またはsearchの内側にその変換を実行し、これは、あなたが関数にStringClassを渡すことはできません意味が。

脇に、変数listを呼び出すべきではありません。それは組み込みの型で、その宣言の後に、組み込みのバージョンlistを隠します。

+0

StringClassクラスの__eq__メソッドで変換を実行できますか? –

+0

あなたは好きな場所でそれを行うことができます。それは良い場所として私を攻撃しないが。主に '__eq__'関数の許容入力を制限し、その意味を変更するためです。それを使用している人なら、 '__eq__'は' StringClass'の 'StringClass'と似ています。突然左に 'StringClass'、右に' str'がなければならない場合は、後でこの区別を忘れてしまったいくつかの不思議な誤りがあるかもしれません。 –

関連する問題